C#如何获取对象的属性

我先定义了一个对象:

public class VXMLData : INotifyPropertyChanged 
    {
        public VXMLData()
        {
            MDSTATE = ModelState.ADD;
        }
string _Zj;
        /// <summary>
        /// 主键 - Zj     主键
        /// </summary>
        [JsonProperty("CDUC001.000")]
        public string Zj
        {
            get { return _Zj; }
            set { _Zj = value; OnPropertyChanged("Zj"); }
        }
        string _Djrid;
        /// <summary>
        /// 登记人ID - Djrid     登记人ID
        /// </summary>
        [JsonProperty("CDUC001.001")]
        public string Djrid
        {
            get { return _Djrid; }
            set { _Djrid = value; OnPropertyChanged("Djrid"); }
        }
}

若要获取“登记人ID”的类型,这个很简单。网上都有方法。但若要获取“登记人ID”的Json类型中的值(CDUC001.001)就有点麻烦。我找了好久终于找到方法了:

第一步:

var properties = typeof(VXMLData).GetProperties();
VXMLData  ryxx = new VXMLData()

 

第二步:循环查找

for (int i = 0; i < properties.Count(); i++)
{ object[] cc = properties[i].GetCustomAttributes(false);
    string _jsonName = ((Newtonsoft.Json.JsonPropertyAttribute)cc[0]).PropertyName;
}
((Newtonsoft.Json.JsonPropertyAttribute)cc[0]).PropertyName;  这段就是获取Json类型名的语句

 

posted @ 2023-08-31 15:23  绝版佳嫐  阅读(102)  评论(0编辑  收藏  举报