How to add data annotations to partial class?

前端 未结 2 2078
广开言路
广开言路 2020-12-28 19:47

I have an auto generated class with a property on it. I want to add some data annotations to that property in another partial class of the same type. How would I do that?<

2条回答
  •  天涯浪人
    2020-12-28 20:14

    Looks like I figured out a different way similar to the link above using MetadataTypeAttribute:

    namespace MyApp.BusinessObjects
    {
        [MetadataTypeAttribute(typeof(SomeClass.Metadata))]{
        public partial class SomeClass
        {
            internal sealed class Metadata
            {
                private Metadata()
                {
                }
    
                [Required]
                public string Name{ get; set; }
            }
        }
    }
    

提交回复
热议问题