在不知道后面的类的情况下获取C#中特定对象属性的值

时间:2020-12-14 21:32:38

I have an object (.NET) of type "object". I don't know the "real type (class)" behind it during runtime , but I know, that the object has a property "string name". How can I retrive the value of "name"? Is this possible?

我有一个类型为“object”的对象(.NET)。我不知道运行时它背后的“真实类型(类)”,但我知道,该对象具有属性“字符串名称”。我怎样才能追溯“名字”的价值?这可能吗?

something like this:

像这样的东西:

object item = AnyFunction(....);
string value = item.name;

6 个解决方案

#1


29  

Use reflection

System.Reflection.PropertyInfo pi = item.GetType().GetProperty("name");
String name = (String)(pi.GetValue(item, null));

#2


27  

You can do it using dynamic instead of object:

你可以使用动态而不是对象来做到这一点:

dynamic item = AnyFuncton(....);
string value = item.name;

#3


4  

Reflection can help you.

反思可以帮到你。

var someObject;
var propertyName = "PropertyWhichValueYouWantToKnow";
var propertyName = someObject.GetType().GetProperty(propertyName).GetValue(someObject, null);

#4


2  

Reflection and dynamic value access are correct solutions to this question but are quite slow. If your want something faster then you can create dynamic method using expressions:

反思和动态价值访问是这个问题的正确解决方案,但速度很慢。如果您想要更快的东西,那么您可以使用表达式创建动态方法:

  object value = GetValue();
  string propertyName = "MyProperty";

  var parameter = Expression.Parameter(typeof(object));
  var cast = Expression.Convert(parameter, value.GetType());
  var propertyGetter = Expression.Property(cast, propertyName);
  var castResult = Expression.Convert(propertyGetter, typeof(object));//for boxing

  var propertyRetriver = Expression.Lambda<Func<object, object>>(castResult, parameter).Compile();

 var retrivedPropertyValue = propertyRetriver(value);

This way is faster if you cache created functions. For instance in dictionary where key would be the actual type of object assuming that property name is not changing or some combination of type and property name.

如果缓存创建的函数,这种方式会更快。例如,在字典中,键是实际的对象类型,假设属性名称没有更改,或者类型和属性名称的某种组合。

#5


0  

Simply try this for all properties of an object,

只需对对象的所有属性进行尝试,

foreach (var prop in myobject.GetType().GetProperties(BindingFlags.Public|BindingFlags.Instance))
{
   var propertyName = prop.Name;
   var propertyValue = myobject.GetType().GetProperty(propertyName).GetValue(myobject, null);

   //Debug.Print(prop.Name);
   //Debug.Print(Functions.convertNullableToString(propertyValue));

   Debug.Print(string.Format("Property Name={0} , Value={1}", prop.Name, Functions.convertNullableToString(propertyValue)));
}

NOTE: Functions.convertNullableToString() is custom function using for convert NULL value into string.empty.

注意:Functions.convertNullableToString()是用于将NULL值转换为string.empty的自定义函数。

#6


0  

In some cases, Reflection doesn't work properly.

在某些情况下,Reflection无法正常工作。

You could use dictionaries, if all item types are the same. For instance, if your items are strings :

如果所有项目类型相同,您可以使用词典。例如,如果您的商品是字符串:

Dictionary<string, string> response = JsonConvert.DeserializeObject<Dictionary<string, string>>(item);

Or ints:

Dictionary<string, int> response = JsonConvert.DeserializeObject<Dictionary<string, int>>(item);

#1


29  

Use reflection

System.Reflection.PropertyInfo pi = item.GetType().GetProperty("name");
String name = (String)(pi.GetValue(item, null));

#2


27  

You can do it using dynamic instead of object:

你可以使用动态而不是对象来做到这一点:

dynamic item = AnyFuncton(....);
string value = item.name;

#3


4  

Reflection can help you.

反思可以帮到你。

var someObject;
var propertyName = "PropertyWhichValueYouWantToKnow";
var propertyName = someObject.GetType().GetProperty(propertyName).GetValue(someObject, null);

#4


2  

Reflection and dynamic value access are correct solutions to this question but are quite slow. If your want something faster then you can create dynamic method using expressions:

反思和动态价值访问是这个问题的正确解决方案,但速度很慢。如果您想要更快的东西,那么您可以使用表达式创建动态方法:

  object value = GetValue();
  string propertyName = "MyProperty";

  var parameter = Expression.Parameter(typeof(object));
  var cast = Expression.Convert(parameter, value.GetType());
  var propertyGetter = Expression.Property(cast, propertyName);
  var castResult = Expression.Convert(propertyGetter, typeof(object));//for boxing

  var propertyRetriver = Expression.Lambda<Func<object, object>>(castResult, parameter).Compile();

 var retrivedPropertyValue = propertyRetriver(value);

This way is faster if you cache created functions. For instance in dictionary where key would be the actual type of object assuming that property name is not changing or some combination of type and property name.

如果缓存创建的函数,这种方式会更快。例如,在字典中,键是实际的对象类型,假设属性名称没有更改,或者类型和属性名称的某种组合。

#5


0  

Simply try this for all properties of an object,

只需对对象的所有属性进行尝试,

foreach (var prop in myobject.GetType().GetProperties(BindingFlags.Public|BindingFlags.Instance))
{
   var propertyName = prop.Name;
   var propertyValue = myobject.GetType().GetProperty(propertyName).GetValue(myobject, null);

   //Debug.Print(prop.Name);
   //Debug.Print(Functions.convertNullableToString(propertyValue));

   Debug.Print(string.Format("Property Name={0} , Value={1}", prop.Name, Functions.convertNullableToString(propertyValue)));
}

NOTE: Functions.convertNullableToString() is custom function using for convert NULL value into string.empty.

注意:Functions.convertNullableToString()是用于将NULL值转换为string.empty的自定义函数。

#6


0  

In some cases, Reflection doesn't work properly.

在某些情况下,Reflection无法正常工作。

You could use dictionaries, if all item types are the same. For instance, if your items are strings :

如果所有项目类型相同,您可以使用词典。例如,如果您的商品是字符串:

Dictionary<string, string> response = JsonConvert.DeserializeObject<Dictionary<string, string>>(item);

Or ints:

Dictionary<string, int> response = JsonConvert.DeserializeObject<Dictionary<string, int>>(item);