代码之家  ›  专栏  ›  技术社区  ›  Chris McCall

我可以在分部类中定义属性,然后在另一个分部类中用属性标记它们吗?

  •  62
  • Chris McCall  · 技术社区  · 14 年前

    public partial class A 
    {
        public string a { get; set; }
    }
    

    然后在另一个文件中:

    public partial class A 
    {
        [Attribute("etc")]
        public string a { get; set; }
    }
    

    这样我就可以从数据库生成一个类,然后使用一个未生成的文件来标记它?

    5 回复  |  直到 4 年前
        1
  •  34
  •   0xced    7 年前

    我在Scott Guthrie的一篇文章(接近结尾)中看到过类似的事情——不过,我自己没有尝试过。
    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; }
    }
    
        2
  •  89
  •   StayOnTarget Charlie Flowers    6 年前

    public partial class UserProfile
    {
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }
    }
    

    比方说,我想添加一个属性来指定UserId是键。然后我会在另一个文件中创建一个分部类,如下所示:

    [Table("UserProfile")]
    [MetadataType(typeof(UserProfileMetadata))]
    public partial class UserProfile
    {
        internal sealed class UserProfileMetadata
        {
            [Key]
            [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
            public int UserId { get; set; }
        }
    }
    
        3
  •  2
  •   CyberNinja    8 年前

    这是我的答案
    不同的类文件,或者您可以将元数据组合在同一个文件中,但保持名称空间相同..这样它们就可以清楚地看到彼此。

    请记住,当您更新模型(如添加更多列)时,也必须更新项目类。

    --your model class
    public partial class A {
        public string a {get; set;}
    }
    
    --your project class 
    public class Ametadata {
         [Attribute("etc")]
         public string a {get; set;}
    }
    
    
    [MetadataType(typeof(Ametadata))]
    public partial class A
    {
    }
    
        4
  •  1
  •   Masoud Darvishian    7 年前

    您需要为您的 A 就像下面的例子

    using System.ComponentModel.DataAnnotations;
    
    // your auto-generated partial class
    public partial class A 
    {
        public string MyProp { get; set; }
    }
    
    [MetadataType(typeof(AMetaData))]
    public partial class A 
    {
    
    }
    
    public class AMetaData
    {
        [System.ComponentModel.DefaultValue(0)]
        public string MyProp { get; set; }
    }
    
        5
  •  0
  •   KeithS    14 年前

    public class A
    {
       public string MyString;
    }
    
    public class AMeta
    {
       [TheAttribute("etc")]
       public object MyString;
    }
    
    ...
    
    var myA = new A();
    var metaType = Type.GetType(myA.GetType().Name + "Meta");
    var attributesOfMyString = metaType.GetMember("MyString").GetCustomAttributes();