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

C#.NetLambda

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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();

Solution 2 - C#

I hope this code will help you  

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace Student
{
    class Program
    {
        static void Main(string[] args)
        {
            var a = new Student();
            PrintProperty(a, "Name");
            PrintProperty(a, "Age");
            Console.ReadKey();

        }
        private static void PrintProperty<T>(T a, string propName)
        {
            PrintProperty<T, object>(a, propName);
        }
        private static void PrintProperty<T, TProperty>(T a, string propName)
        {
            ParameterExpression ep = Expression.Parameter(typeof(T), "x");
            MemberExpression em = Expression.Property(ep, typeof(T).GetProperty(propName));
            var el = Expression.Lambda<Func<T, TProperty>>(Expression.Convert(em, typeof(object)), ep);
            Console.WriteLine(GetValue(a, el));
        }

        private static TPorperty GetValue<T, TPorperty>(T v, Expression<Func<T, TPorperty>> expression)
        {
            return expression.Compile().Invoke(v);
        }

        public class Student
        {
            public Student()
            {
                Name = "Albert Einstein";
                Age = 15;
            }
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartin RobinsView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#Zekaee EsmaeelView Answer on Stackoverflow