Can I define properties in partial classes, then mark them with attributes in another partial class?

前端 未结 5 1209
野趣味
野趣味 2020-12-02 06:17

Is there a way I can have a generated code file like so:

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

and then in another file:

5条回答
  •  我在风中等你
    2020-12-02 07:04

    You need to define a partial class for your A class just like below example

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

提交回复
热议问题