C#怎么获取object对象的属性值 (转)

The code is like this:

main()
{

      Car car = new Car();
      var value = GetPropertyValue(car, "name");          
}

 

   public static object GetPropertyValue(object info, string field)
{
if (info == null) return null;

Type t = info.GetType();

IEnumerable<System.Reflection.PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi;
            
return property.First().GetValue(info, null);

        } 
 
posted @ 2014-10-16 16:45  邹邹  Views(6810)  Comments(0)    收藏  举报