예제 #1
0
 /// <summary>
 /// 通过反射机制,获取类对象中的某个属性的值
 /// </summary>
 /// <param name="columnName">属性</param>
 /// <param name="obj">对象</param>
 /// <returns></returns>
 public static string GetModelValue(object obj, string columnName)
 {
     try
     {
         Type   Ts           = obj.GetType();
         string propertyName = MyStringUtil.getCamelName(columnName);
         System.Reflection.PropertyInfo propertyInfo = Ts.GetProperty(propertyName);
         if (propertyInfo != null)
         {
             object o     = propertyInfo.GetValue(obj, null);
             string Value = Convert.ToString(o);
             if (string.IsNullOrEmpty(Value))
             {
                 return(null);
             }
             return(Value);
         }
         else
         {
             return(null);
         }
     }
     catch
     {
         return(null);
     }
 }
예제 #2
0
 public static bool SetModelValue(object obj, string columnName, string Value)
 {
     try
     {
         Type   Ts           = obj.GetType();
         string propertyName = MyStringUtil.getCamelName(columnName);
         System.Reflection.PropertyInfo propertyInfo = Ts.GetProperty(propertyName);
         if (propertyInfo != null)
         {
             object v = Convert.ChangeType(Value, propertyInfo.PropertyType);
             propertyInfo.SetValue(obj, v, null);
             return(true);
         }
         else
         {
             return(false);
         }
     }
     catch
     {
         return(false);
     }
 }