1

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?

0

3 Answers 3

4

C# as operator translates to Expression.TypeAs, and C# cast operators translate to either Expression.Convert or Expression.ConvertChecked. As you can see, all they are of type UnaryExpression, hence you can extract the operand expression by using Operand property. And you can do that in a loop in order to support multiple casts.

Putting it into action, replace

var member = propertyLambda.Body as MemberExpression;

with

var operand = propertyLambda.Body;
while (operand.NodeType == ExpressionType.TypeAs || operand.NodeType == ExpressionType.Convert || operand.NodeType == ExpressionType.ConvertChecked)
    operand = ((UnaryExpression)operand).Operand;
var member = operand as MemberExpression;
1

In your case, you need to look at the operand of the unary expression that is the cast.

((propertyLamdba.Body as UnaryExpression).Operand as MemberExpression).Member

To make it more programmatically secure, you should check your propertyLamba. Something like:

if (propertyLamdba.Body.NodeType == ExpressionType.TypeAs)
    member = ((propertyLamdba.Body as UnaryExpression).Operand as MemberExpression).Member
0

I would recommend this:

PropertyInfo info = t.GetType().GetProperty("SomeProperty",
    BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
// Use these binding flags to get a member of any visibility.

It returns a PropertyInfo for a property of a given name.

If you do not want to handle the name explicitly, use nameof(t.SomeProperty).

You may also find the method .GetProperties useful to get all properties of a specific type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.