我正在通过以下代码检索IEnumerable属性列表: BindingFlags bindingFlag = BindingFlags.Instance | BindingFlags.Public; var dataProperties = typeof(myParentObject).GetProperties(bindingFlag); 然后我迭代列表并检索每个属
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查看GetValue的实现:
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);
}
这里的实际类型是RuntimePropertyInfo(PropertyInfo是一个不为GetValue提供实现的抽象类).
