本文介绍了我可以在部分类中定义属性,然后用另一个部分类中的属性标记它们吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法生成这样的代码文件:

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; }
}

这篇关于我可以在部分类中定义属性,然后用另一个部分类中的属性标记它们吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-24 22:25