Can I define properties in partial classes, then mark them with attributes in another partial class?(我可以在部分类中定义属性,然后用另一个部分类中的属性标记它们吗?)
问题描述
有没有办法生成这样的代码文件:
Is there a way I can have a generated code file like so:
public partial class A
{
public string a { get; set; }
}
然后在另一个文件中:
public partial class A
{
[Attribute("etc")]
public string a { get; set; }
}
这样我就可以从数据库中生成一个类,然后使用一个非生成的文件来标记它?
So that I can have a class generated from the database and then use a non-generated file to mark it up?
推荐答案
我在 Scott Guthrie 的一篇文章中看到过类似的事情(接近结尾)——不过,我自己没有尝试过.
http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx
I've seen something like this done in an article by Scott Guthrie (near the end of it) - didn't try it myself, though.
http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx
[MetadataType(typeof(Person_Validation))]
public partial class Person
{
// Partial class compiled with code produced by VS designer
}
[Bind(Exclude="ID")]
public class Person_Validation
{
[Required(ErrorMessage = "First Name Required")]
[StringLength(50, ErrorMessage = "Must be under 50 characters")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last Name Required")]
[StringLength(50, ErrorMessage = "Must be under 50 characters")]
public string LastName { get; set; }
[Required(ErrorMessage = "Age Required")]
[Range(0, 120, ErrorMessage = "Age must be between 0 and 120")]
public int Age { get; set; }
[Required(ErrorMessage = "Email Required")]
[Email(ErrorMessage = "Not a valid email")]
public string Email { get; set; }
}
这篇关于我可以在部分类中定义属性,然后用另一个部分类中的属性标记它们吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我可以在部分类中定义属性,然后用另一个部分类中的属性标记它们吗?
基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01