我在一些代碼創建此表達式:表達`噸=>(t.SomeProperty作爲對象)`是指一種方法,而不是一個屬性
t => (t.SomeProperty As Object)
和現在正在嘗試獲取屬性-name(SomeProperty
)通過這個小片段(從網上找來的很長一段時間的自我):
public static PropertyInfo GetPropertyInfo<TSource, TProperty>
(TSource source, Expression<Func<TSource, TProperty>> propertyLambda) {
var type = typeof(TSource);
var member = propertyLambda.Body as MemberExpression;
if (member == null)
throw new ArgumentException($"Expression '{propertyLambda}' refers to a method, not a property.");
var propInfo = member.Member as PropertyInfo;
if (propInfo == null)
throw new ArgumentException($"Expression '{propertyLambda}' refers to a field, not a property.");
if (type != propInfo.ReflectedType && !type.IsSubclassOf(propInfo.ReflectedType))
throw new ArgumentException($"Expresion '{propertyLambda}' refers to a property that is not from type {type}.");
return propInfo;
}
但你也知道,propertyLambda.Body as MemberExpression
始終爲空,導致此:t => (t.Some1 As Object)
不是財產指。在這種情況下,有沒有辦法獲得物業名稱?