我正在通过以下代码检索IEnumerable属性列表:
BindingFlags bindingFlag = BindingFlags.Instance | BindingFlags.Public; var dataProperties = typeof(myParentObject).GetProperties(bindingFlag);
然后我将遍历列表并检索每个属性的值。
我遇到过两种不同的方法来实现这一点,只是想知道它们之间的区别是什么:
1)
object propertyValue = property.GetGetMethod().Invoke(myObject, null);
2)
object propertValue = property.GetValue(myObject, null)
事实上,没有区别。您可以看到 方法 使用 Reflector :
public override object GetValue(object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture) { MethodInfo getMethod = this.GetGetMethod(true); if (getMethod == null) { throw new ArgumentException( Environment.GetResourceString("Arg_GetMethNotFnd")); } return getMethod.Invoke(obj, invokeAttr, binder, index, null); }
这里的实际类型是 运行时属性信息 ( PropertyInfo 是不提供实现的抽象类 方法 )