简体   繁体   English

类型'System.Int32'的表达式不能用于返回类型'System.Object'

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

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. 我在过去做过这个没有问题的反射,但我现在正在尝试使用Lambda函数,以便我可以缓存函数以便重用。

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. 每当我尝试访问Int32类型的属性时,我都会收到此消息标题中显示的异常 - 但我也是为Nullable类型和其他类型获取它。 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? 任何人都可以建议我在保持Lambda功能的同时以不同的方式解决这个问题,以便我可以缓存访问器吗?

Have you tried using Expression.Convert ? 你尝试过使用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();

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Linq和Equality Operator:类型'System.Int32'的表达式不能用于'System.Object'类型的参数 - Linq and the Equality Operator: Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' 类型'System.Int32'的表达式不能用于方法'Boolean Equals(System.Object)'的'System.Object'类型的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)' 类型&#39;System.Int32&#39;的表达式不能用于方法&#39;Boolean Equals(System.Object)&#39;的类型&#39;System.Object&#39;的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)' 从字符串构建 Linq 排序表达式会导致“Expression of "system.int32" can not be used for return type "System.Object"” - Building a Linq sort expression from string results in 'Expression of "system.int32" can not be used for return type "System.Object" ' &#39;System.Int16&#39;类型的表达式不能用于返回类型&#39;System.Object&#39; - Expression of type 'System.Int16' cannot be used for return type 'System.Object' “System.Int64”类型的表达式不能用于返回类型“System.Object” - Expression of type 'System.Int64' cannot be used for return type 'System.Object' Expression.Convert:'System.Int64'类型的对象无法转换为'System.Int32'类型 - Expression.Convert: Object of type 'System.Int64' cannot be converted to type 'System.Int32' 类型&#39;System.Nullable`1 [System.Int32]&#39;的表达式不能用于类型&#39;System.Int32&#39;\\ r \\ n的构造函数参数名称:arguments [0] - Expression of type 'System.Nullable`1[System.Int32]' cannot be used for constructor parameter of type 'System.Int32'\r\nParameter name: arguments[0] &#39;System.DateTime&#39;类型的表达式不能用于返回类型&#39;System.Object&#39; - Expression of type 'System.DateTime' cannot be used for return type 'System.Object' 无法将类型为“WhereListIterator`1[System.Object]”的对象转换为类型“System.Collections.Generic.IEnumerable`1[System.Int32]” - Unable to cast object of type 'WhereListIterator`1[System.Object]' to type 'System.Collections.Generic.IEnumerable`1[System.Int32]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM