System.Reflection GetProperties method not returning values

前端 未结 4 1156
情书的邮戳
情书的邮戳 2020-12-17 08:19

Can some one explain to me why the GetProperties method would not return public values if the class is setup as follows.

public class DocumentA
         


        
4条回答
  •  醉酒成梦
    2020-12-17 08:56

    You haven't declared any properties - you've declared fields. Here's similar code with properties:

    public class DocumentA
    {
        public string AgencyNumber { get; set; }
        public bool Description { get; set; }
        public bool Establishment { get; set; }
    
        public DocumentA() 
        {
            AgencyNumber = "";
        }
    }
    

    I would strongly advise you to use properties as above (or possibly with more restricted setters) instead of just changing to use Type.GetFields. Public fields violate encapsulation. (Public mutable properties aren't great on the encapsulation front, but at least they give an API, the implementation of which can be changed later.)

提交回复
热议问题