public class A_domain { [StringLength(50)] public string Title { get; set; } ... } public class A_view { public string Title { get; set; } } 

How can I copy the "StringLength" attribute in A_view? So always be sure that the data does not differ. At the moment I did this:

 public class A_domain { public const int MaxTitleLength = 50; [StringLength(MaxTitleLength)] public string Title { get; set; } ... } public class A_view { [StringLength(A_domain.MaxTitleLength)] public string Title { get; set; } } 

Of course it works, but it seems to me that there is some more correct / beautiful solution (because when there are more than one such field, it forces to write a lot of superfluous when that distracts from the essence).

And then how to get this value?

 void foo() { A_view a = new A_view(); a.Title //get max length } 

    1 answer 1

    Yes, this is not correct. If there is a connection between your classes, then this connection should be designated programmatically, i.e. through a related class or interface. For example:

     public class Element { [StringLength(50)] public string Title { get; set; } } 

    And inherit this class:

     public class A_view:Element {} public class A_domain:Element {} 

    If you need to get the number 50 from the attribute read the article about extracting information from the attributes from Microsoft