I have created this expression in some code:
t => (t.SomeProperty As Object)
and now I'm trying to get the property-name (SomeProperty
) by this little snippet (got from Internet a long time ego):
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;
}
But as you know, propertyLambda.Body as MemberExpression
is always null, cause this: t => (t.Some1 As Object)
is not a property-refer. Is there any way to get the property-name in this case?