Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why could an Expression's Body in C#.net not use properties of type int, double or bool?

I have a function:

private string GetPropertyName(Expression<Func<object, object>> f) {
    if ((f.Body as MemberExpression) != null) {
        return (f.Body as MemberExpression).Member.Name;
    }
    return "";
}

And it is used this way:

string x1 = GetPropertyName(x => Property1);
string x2 = GetPropertyName(x => Property2);
string x3 = GetPropertyName(x => Property3);

where Property1 is an int, Property2 is a string, and Property3 is an object...

Only the names of Property2 and Property3 of types string and object respectfully are correctly returned, but Property1's f.Body as MemberExpression is null...

Why is this so, and how can we change the code so the function returns the Properties' names correctly?

like image 627
Jronny Avatar asked Dec 17 '25 22:12

Jronny


2 Answers

I guess the problem is that x => Property1 expression contains implicit cast to object. Compiler adds such cast to fulfil arguments of GetPropertyName.

You should check for this cast

private string GetPropertyName(Expression<Func<object, object>> f) {
    var body = f.Body;
    if (body.NodeType==ExpressionType.Convert)
      body = ((UnaryExpression) body).Operand;
    if ((body as MemberExpression) != null) {
        return (body as MemberExpression).Member.Name;
    }
    return "";
}
like image 102
Denis K Avatar answered Dec 20 '25 11:12

Denis K


There is a boxing operation happening under the covers: The int is boxed in order to pass as an object.

To avoid further problems, I'd recommend changing the signature of your method and make it generic:

private string GetPropertyName<T, TResult>(Expression<Func<T, TResult>> f) {
    // ...
}

This is entirely untested but it should resolve your issue; The use of generics should avoid the necessity to box or convert anything (implicitly).

like image 36
Bryan Menard Avatar answered Dec 20 '25 11:12

Bryan Menard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!