当只在运行时知道Type时,如何使用表达式树来调用泛型方法?

时间:2010-03-12 14:41:35

标签: c# lambda

这是我用反射解决的问题,但我想看看如何使用表达式树来完成它。

我有一个通用功能:

private void DoSomeThing<T>( param object[] args ) {
    // Some work is done here.
}

我需要在班上的其他地方打电话。现在,通常情况下,这很简单:

DoSomeThing<int>( blah );

但我必须知道,在设计时我正在使用int。当我不知道类型,直到运行时我需要帮助。就像我说的,我知道如何通过反射来做到这一点,但我想通过表达式树来做,因为我(非常有限)的理解是我可以这样做。

我可以获得这种理解的网站的任何建议或指向,最好是示例代码?

2 个答案:

答案 0 :(得分:6)

MethodInfo.MakeGenericMethod

然后只需创建一个委托并调用它。(当然不是表达式; p)

<强>更新

通常,我更喜欢使用泛型类型,Activator.CreateInstance只需要更少的工作。一切都取决于你的情况。

答案 1 :(得分:5)

是的,可以通过表达式树来完成。优点是你得到一个代表,所以重复调用将比一遍又一遍MethodInfo.Invoke()快得多。 dynamic关键字也可以执行此操作。

示例:

What type would you like to use?
decimal
Selected type 'System.Decimal'
Input Value:
5.47
<<<USING object>>>
The object has static type 'System.Object',  dynamic type 'System.Decimal', and value '5.47'
<<<USING dynamic>>>
The object has static type 'System.Decimal',  dynamic type 'System.Decimal', and value '5.47'
<<<USING reflection>>>
The object has static type 'System.Decimal',  dynamic type 'System.Decimal', and value '5.47'
<<<USING expression tree>>>
The object has static type 'System.Decimal',  dynamic type 'System.Decimal', and value '5.47'

代码:

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

namespace SO2433436
{
    class Program
    {
        static void LogObject<T>(T t)
        {
            Console.WriteLine("The object has static type '" + typeof(T).FullName + "',  dynamic type '" + t.GetType() + "', and value '" + t.ToString() + "'");
        }

        static void Main(string[] args)
        {
            Console.WriteLine("What type would you like to use?");
            string typeName = Console.ReadLine();

            Type userType;
            switch (typeName)
            {
                case "byte": userType = typeof(byte); break;
                case "sbyte": userType = typeof(sbyte); break;
                case "ushort": userType = typeof(ushort); break;
                case "short": userType = typeof(short); break;
                case "uint": userType = typeof(uint); break;
                case "int": userType = typeof(int); break;
                case "string": userType = typeof(string); break;
                case "decimal": userType = typeof(decimal); break;
                default:
                    userType = Type.GetType(typeName);
                    break;
            }

            Console.WriteLine("Selected type '" + userType.ToString() + "'");

            Console.WriteLine("Input Value:");
            string val = Console.ReadLine();

            object o = TypeDescriptor.GetConverter(userType).ConvertFrom(val);

            Console.WriteLine("<<<USING object>>>");
            LogObject(o);

            Console.WriteLine("<<<USING dynamic>>>");
            LogObject((dynamic)o);

            Console.WriteLine("<<<USING reflection>>>");
            Action<object> f = LogObject<object>;
            MethodInfo logger = f.Method.GetGenericMethodDefinition().MakeGenericMethod(userType);
            logger.Invoke(null, new[] { o });

            Console.WriteLine("<<<USING expression tree>>>");
            var p = new[] { Expression.Parameter(typeof(object)) };
            Expression<Action<object>> e =
                Expression.Lambda<Action<object>>(
                    Expression.Call(null,
                                    logger,
                                    Expression.Convert(p[0], userType)
                                   )
                , p);
            Action<object> a = e.Compile();
            a(o);
        }
    }
}