我有一个“object”类型的对象 (.NET)。我不知道在运行时它背后的“真实类型(类)”,但我知道,该对象有一个属性“字符串名称”。我怎样才能取回“名字”的值(value)?这可能吗?
像这样:
object item = AnyFunction(....);
string value = item.name;
最佳答案
使用反射
System.Reflection.PropertyInfo pi = item.GetType().GetProperty("name");
String name = (String)(pi.GetValue(item, null));
关于c# - 在不知道背后的类的情况下获取 C# 中特定对象属性的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11395315/