DefaultValue attribute is not working with my Auto Property

后端 未结 3 1573
迷失自我
迷失自我 2020-12-09 16:21

I have the following Auto Property

[DefaultValue(true)]
public bool RetrieveAllInfo { get; set; }

when I try to use it inside the code i f

3条回答
  •  眼角桃花
    2020-12-09 16:53

    One hack for this is on this link.

    In short, call this function at the end of constructor.

    static public void ApplyDefaultValues(object self)
       {
            foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(self)) {
                DefaultValueAttribute attr = prop.Attributes[typeof(DefaultValueAttribute)] as DefaultValueAttribute;
                if (attr == null) continue;
                prop.SetValue(self, attr.Value);
            }
       }
    

提交回复
热议问题