3

我有一个小的依赖注入框架,我试图让它Lazy<>动态地解析实例。这个想法是做这样的事情:

DIContainer.Register<IDbCommand,SqlCommand>();

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

前几天我读到 Autofac 能够做到这一点。

我一直试图为该Lazy<>实例设置构造函数。在下一个测试代码中,抛出异常,因为所需的类型构造函数需要 a Func<arg>,但我传递的是 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);
    }

我对如何创建适合Lazy<>构造函数参数的委托一无所知。任何的想法?

干杯。

4

3 回答 3

3

这不是微不足道的。一种可能的解决方案是使用反射:

  1. 创建一个泛型ResolveType方法:

    public static T ResolveType<T>()
    {
        return (T)ResolveType(typeof(T));
    }
    
  2. 创建一个使用此方法的委托:

    // 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. 使用该委托:

    return Activator.CreateInstance(_lazyType.MakeGenericType(arg), delegate);
    
于 2013-10-23T15:25:34.080 回答
2

这个应用程序输出“真”和“0”。即ResolveTest(typeof(Lazy<int>))返回一个Lazy<int>对象,按照你想要的方式构造。

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

这是一种重写ResolveTest为泛型的方法Resolve<T>(例如Resolve<int>返回Lazy<int>)。这有点不同,因为没有等价于ResolveTest(typeof(int)),它返回一个int

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

或使用泛型ResolveType<T>

static Lazy<T> Resolve<T>()
{
    return new Lazy<T>(() => ResolveType<T>());
}
public static T ResolveType<T>()
{
    return Activator.CreateInstance<T>();
}
于 2013-10-23T15:42:29.630 回答
1
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);
}
于 2013-10-23T17:09:14.503 回答