Exemplo n.º 1
0
 /// <summary>
 /// 获取需要保存到数据库的属性
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public static JsonMemberAttribute GetJsonMember(this Type type, string propertyName)
 {
     lock (JsonMemberCache)
     {
         if (JsonMemberCache.ContainsKey(type))
         {
             return(JsonMemberCache[type].First(p => p.Name == propertyName));
         }
         else
         {
             var tpinfos = type.GetProperties();
             List <JsonMemberAttribute> rpinfos = new List <JsonMemberAttribute>();
             for (int i = tpinfos.Length - 1; i >= 0; i--)
             {
                 PropertyInfo pinfo = tpinfos[i];
                 var          attr  = pinfo.GetCustomAttributes(typeof(JsonMemberAttribute), true);
                 if (attr != null && attr.Length >= 1)
                 {
                     JsonMemberAttribute dpa = attr[0] as JsonMemberAttribute;
                     dpa.PropertyType = pinfo.PropertyType;
                     dpa.PropertyInfo = pinfo;
                     rpinfos.Add(dpa);
                 }
             }
             rpinfos = rpinfos.OrderBy(p => p.ShowIndex).ToList();
             JsonMemberCache[type] = rpinfos;
             return(rpinfos.First(p => p.Name == propertyName));
         }
     }
 }
Exemplo n.º 2
0
    public static object GetValueByJsonPropertyName(this object instance, string propertyName)
    {
        string[]            ts     = propertyName.Split('.');
        JsonMemberAttribute result = null;
        object tempValue           = instance;

        for (int i = 0; i < ts.Length; i++)
        {
            string tpn = ts[i];
            result    = tempValue.GetType().GetJsonMember(tpn);
            tempValue = result.GetValue(tempValue, false);
        }
        return(tempValue);
    }
Exemplo n.º 3
0
    public static void SetValueByJsonPropertyName(this object instance, string propertyName, object value)
    {
        string[]            ts          = propertyName.Split('.');
        JsonMemberAttribute resultPinfo = null;
        object tempValue    = instance;
        object parentObject = null;

        for (int i = 0; i < ts.Length; i++)
        {
            string tpn = ts[i];
            resultPinfo  = tempValue.GetType().GetJsonMember(tpn);
            parentObject = tempValue;
            tempValue    = resultPinfo.GetValue(tempValue, false);
        }
        resultPinfo.SetValue(parentObject, value);
    }