Home » c# » Expression of type 'System.Int32' cannot be used for return type 'System.Object'

Expression of type 'System.Int32' cannot be used for return type 'System.Object'

Posted by: admin December 5, 2017 Leave a comment

Questions:

I am trying to produce a simple scripting system that will be used to print labels. I have done this in the past with reflection with no problem, but I am now trying to do it with Lambda functions so that I can cache the functions for reuse.

The code I have so far is as follows…

public static string GetValue<T>(T source, string propertyPath) {

    try {

        Func<T, Object> func;

        Type type = typeof(T);
        ParameterExpression parameterExpression = Expression.Parameter(type, @"source");
        Expression expression = parameterExpression;
        foreach (string property in propertyPath.Split('.')) {
            PropertyInfo propertyInfo = type.GetProperty(property);
            expression = Expression.Property(expression, propertyInfo);
            type = propertyInfo.PropertyType;
        }

        func = Expression.Lambda<Func<T, Object>>(expression, parameterExpression).Compile();

        object value = func.Invoke(source);
        if (value == null)
            return string.Empty;
        return value.ToString();

    }
    catch {

        return propertyPath;

    }

}

This seems to work in some cases, but in others it fails. The problem seems to be in my trying to return the values as objects – irrespective of the actual data types. I am trying to do this because I do not know at compile time what the data type will be but in the long run, I only need a string.

I am getting the exception shown in the title of this message whenever I try to access a property of type Int32 – but I am also getting it for Nullable types and others.
The exception is thrown when I try to compile the expression into the function.

Can anybody suggest how I might go about this differently whilst maintaining the Lambda functionality so that I can cache the accessors?

Answers:

Have you tried using Expression.Convert? That will add the boxing/lifting/etc conversion.

Expression conversion = Expression.Convert(expression, typeof(object));
func = Expression.Lambda<Func<T, Object>>(conversion, parameterExpression).Compile();

Leave a Reply

Your email address will not be published. Required fields are marked *