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

发布于 2024-08-20 10:09:46 字数 1252 浏览 3 评论 0原文

我正在尝试制作一个简单的脚本系统,用于打印标签。我过去通过反射完成此操作没有任何问题,但我现在尝试使用 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 类型的属性时,我都会收到此消息标题中显示的异常 - 但我也会收到可为 Null 类型和其他类型的异常。 当我尝试将表达式编译到函数中时抛出异常。

谁能建议我如何以不同的方式解决这个问题,同时维护 Lambda 功能,以便我可以缓存访问器?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

菩提树下叶撕阳。 2024-08-27 10:09:46

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

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

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();
只涨不跌 2024-08-27 10:09:46

我希望这段代码能帮助你

using System;
using System.Linq.Expressions;

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

        private static void PrintProperty<T>(T obj, string propName)
        {
            PrintProperty<T, object>(obj, propName);
        }

        private static void PrintProperty<T, TProperty>(T obj, 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(obj, el));
        }

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

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

I hope this code will help you

using System;
using System.Linq.Expressions;

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

        private static void PrintProperty<T>(T obj, string propName)
        {
            PrintProperty<T, object>(obj, propName);
        }

        private static void PrintProperty<T, TProperty>(T obj, 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(obj, el));
        }

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

        public class Student
        {
            public Student()
            {
                Name = "Albert Einstein";
                Age = 15;
            }
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文