How to create a Func<> delegate programmatically

孤者浪人 提交于 2020-01-02 08:07:09

问题


I have a small dependency injection framework, and I am trying to make it resolve Lazy<> instances dynamically. The idea is to do something like that:

DIContainer.Register<IDbCommand,SqlCommand>();

var lazyCommand = DIContainer.Resolve<Lazy<IDbCommand>>();

I read the other day that Autofac was able of doing that.

I am stuck trying to set the constructor for that Lazy<> instance. In the next test code, a exception is thrown because the desired type constructor is expecting a Func<arg>, but I am passing a Func<Object>:

    static readonly Type _lazyType = typeof(Lazy<>);
    static Object ResolveTest(Type type)
    {
        if (type.IsGenericType && type.GetGenericTypeDefinition() == _lazyType)
        {
            var arg = type.GetGenericArguments()[0];

            return Activator.CreateInstance(_lazyType.MakeGenericType(arg), new Func<Object>(() => ResolveType(arg)));
        }
        else 
            return ResolveType(type);
    }

I am out of ideas about how to create a delegate that fits for the Lazy<> constructor parameter. Any idea?

Cheers.


回答1:


That's not trivial. One possible solution would be to work with reflection:

  1. Create a generic ResolveType method:

    public static T ResolveType<T>()
    {
        return (T)ResolveType(typeof(T));
    }
    
  2. Create a delegate that uses this method:

    // You probably want to cache this MethodInfo:
    var method = typeof(TypeContainingResolveType)
                     .GetMethods()
                     .Single(x => x.IsGenericMethod && 
                                  x.Name == "ResolveType")
                     .MakeGenericMethod(arg);
    
    var delegate = Delegate.CreateDelegate(
                       typeof(Func<>).MakeGenericType(arg),
                       method);
    
  3. Use that delegate:

    return Activator.CreateInstance(_lazyType.MakeGenericType(arg), delegate);
    



回答2:


This app outputs "True" and "0". I.e. ResolveTest(typeof(Lazy<int>)) returns a Lazy<int> object, constructed like you wanted.

using System;
using System.Linq.Expressions;

namespace TestApp
{
    public class Class1
    {
        public static void Main()
        {
            object lazyInt = ResolveTest(typeof(Lazy<int>));
            Console.WriteLine(lazyInt.GetType() == typeof(Lazy<int>));
            Console.WriteLine(((Lazy<int>)lazyInt).Value);
        }

        static readonly Type _lazyType = typeof(Lazy<>);
        static Object ResolveTest(Type type)
        {
            if (type.IsGenericType && type.GetGenericTypeDefinition() == _lazyType)
            {
                var arg = type.GetGenericArguments()[0];
                var lazyArgType = _lazyType.MakeGenericType(arg);
                var funcArgType = typeof(Func<>).MakeGenericType(arg);
                var funcCtor = lazyArgType.GetConstructor(new[] { funcArgType });
                Expression<Func<object>> f = () => ResolveTest(arg);
                var func = typeof(Class1).GetMethod("BuildCastedThing").MakeGenericMethod(arg).Invoke(null, new[] { f });
                var arguments = new object[] { func };

                var retVal = funcCtor.Invoke(arguments);
                return retVal;
            }
            else
                return ResolveType(type);
        }
        public static object ResolveType(Type type)
        {
            return Activator.CreateInstance(type);
        }
        public static Func<T> BuildCastedThing<T>(Expression<Func<object>> f)
        {
            Expression<Func<T>> expr =
                Expression.Lambda<Func<T>>(
                    Expression.Convert(
                        Expression.Invoke(f),
                        typeof(T)));

            return expr.Compile();
        }
    }
}

This is a way to rewrite ResolveTest as a generic Resolve<T> (e.g. Resolve<int> returns Lazy<int>). This is a little different, since there's no equivalent to ResolveTest(typeof(int)), which returns an int.

static Lazy<T> Resolve<T>()
{
    var arg = typeof(T);
    return new Lazy<T>(() => (T)ResolveType(arg));
}

Or with a generic ResolveType<T>:

static Lazy<T> Resolve<T>()
{
    return new Lazy<T>(() => ResolveType<T>());
}
public static T ResolveType<T>()
{
    return Activator.CreateInstance<T>();
}



回答3:


public static Object ResolveTest(Type type)
{
    if (type.IsGenericType && type.GetGenericTypeDefinition() == _lazyType)
    {
        var arg = type.GetGenericArguments()[0];

        Expression<Func<object>> expressionWithFuncOfTypeObject = () => ResolveType(arg);
        UnaryExpression expressionThatEvaluatesToAnObjectOfTypeArg = Expression.Convert(expressionWithFuncOfTypeObject.Body, arg);
        LambdaExpression expressionWithFuncOfTypeArg = Expression.Lambda(typeof(Func<>).MakeGenericType(arg), expressionThatEvaluatesToAnObjectOfTypeArg);
        Delegate funcOfTypeArg = expressionWithFuncOfTypeArg.Compile(); // <-- At runtime this will be of type Func<T>

        return Activator.CreateInstance(_lazyType.MakeGenericType(arg), funcOfTypeArg);
    }
    else
        return ResolveType(type);
}


来源:https://stackoverflow.com/questions/19545539/how-to-create-a-func-delegate-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!