【问题标题】:Expression of type 'System.Int32' cannot be used for return type 'System.Object'“System.Int32”类型的表达式不能用于返回类型“System.Object”
【发布时间】:2011-01-13 02:24:34
【问题描述】:

我正在尝试制作一个用于打印标签的简单脚本系统。我过去通过反射没有问题地做到了这一点,但我现在尝试使用 Lambda 函数来做到这一点,以便我可以缓存函数以供重用。

我目前的代码如下...

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;

    }

}

这似乎在某些情况下有效,但在其他情况下却失败了。问题似乎在于我试图将值作为对象返回 - 无论实际数据类型如何。我正在尝试这样做,因为我在编译时不知道数据类型是什么,但从长远来看,我只需要一个字符串。

每当我尝试访问 Int32 类型的属性时,我都会收到此消息标题中显示的异常 - 但对于 Nullable 类型和其他类型,我也会收到此异常。 当我尝试将表达式编译到函数中时抛出异常。

任何人都可以建议我如何在保持 Lambda 功能的同时以不同的方式解决这个问题,以便我可以缓存访问器吗?

【问题讨论】:

    标签: c# .net lambda


    【解决方案1】:

    您是否尝试过使用Expression.Convert?这将添加拳击/举重/等转换。

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

    【讨论】:

    • 您非常接近这个答案,只是之前需要转换的是“表达式”而不是“parameterExpression”(表达式 = Expression.Convert(expression, typeof(Object)); )汇编。谢谢。
    • 非常感谢! :)
    • @JonSkeet:你会如何为属性设置器做同样的事情?我已经尝试了所有我能想到的以相反方向进行转换的表达式操作,但没有任何效果?
    • @JoshuaFrank:我建议您提出一个新问题,并提供一个完整示例来说明您正在尝试做什么。大多数情况下,您希望使用一个属性链,其中除了最后一个表达式之外的所有表达式仍然是一个 getter - 只有 last 部分是一个 setter。但是,如果没有尝试解决的示例,很难比这更具体。
    【解决方案2】:

    我希望这段代码对你有帮助

    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; }
            }
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      相关资源
      最近更新 更多