3

我想发出一个返回 Func<> 的方法。在这个方法中,我必须创建一个完全服务于返回类型的委托或 lambda 表达式。

总而言之,它应该是这样的:

// I have a resolve method that will be called inside my missing method
// This is it's signature:
object Resolve( params object[] args);

// This is how I use it:
var barFactory = ( Func<IBar> )MissingMethod( typeof( IBar ) );
var bar = barFactory.Invoke();

// or - with one string argument:
var fooFactory = ( Func<string, IFoo> )MissingMethod( typeof( IFoo ), typeof( string ) );
var foo = fooFactory.Invoke( "argument for foo" );

在 MissingMethod() 内部,它应该如下所示:

object MissingMethod( Type returnType, params Type[] argTypes )
{
  // Create the type of Func<> based on the passed returnType and the argTypes
  var funcType = typeof(Func<,...,>).MakeGenericType( ... )

  // Here I have to use the Resolve() method and cast the lambda to the correct type
  return (cast to funcType)( (arg1, arg2) => Resolve( arg1, arg2 ) );
}

我认为获得我的 MissingMethod() 的唯一方法是使用反射.emit。

你知道关于发出 lambda 或委托的好资源或教程吗?

您是否看到此问题的另一种可能的解决方案?

编辑:
这是我想要实现的场景:

static void Main()
{
  var container = new Container();
  container.Register<Foo>();
  container.Register<ConsumerClass>();

  var consumerClass = Container.Resolve<ConsumerClass>();
}

class Foo()
{
  public Foo( string argument ) {}
}

class ConsumerClass
{
  public ConsumerClass( [Inject] Func<string, Foo> factory )
  {
    var foo1 = factory.Invoke( "first foo" );
    var foo2 = factory.Invoke( "another foo" );
    // ...
  }
}

我正在尝试实现 Container 和 Resolve() 方法。我知道注册了一个“Foo”类型。而且我知道它的构造函数需要一个字符串才能被调用。

当我必须解析“ConsumerClass”类型时,我看到它想要注入一个 Func。这不完全是我的容器可以提供的,因为通常它为 Foo 提供单个实例,如下所示:

Container.Resolve<Foo>( "argument" );

但是尽管如此,容器也应该能够提供 Func。它有所有需要的信息。

但是现在我被困在创建这个绑定的 Func<,> 中。请记住,它也可能是 Func<,,,>。因此,我正在寻找一种可以即时创建我的 this 代表的解决方案。它们必须可以转换为确切的绑定类型。

编辑:
我不知道如何更好地描述它......我正在尝试做这样的事情。但我不想通过目标。代替

delegate void object LateBoundMethod( object target, object[] arguments );

我的代表应该看起来像

delegate void object LateBoundMethod( object[] arguments );

并且目标作为实例字段提供。通过采用和“改进”Marc 的解决方案,我得到:

private Delegate CreateDelegate( Type returnType, Type[] parameterTypes )
{
  m_Type = returnType;

  var i = 0;
  var param = Array.ConvertAll( parameterTypes, arg => Expression.Parameter( arg, "arg" + i++ ) );
  var asObj = Array.ConvertAll( param, p => Expression.Convert( p, typeof( object ) ) );
  var argsArray = Expression.NewArrayInit( typeof( object ), asObj );

  var callEx = Expression.Call( null, typeof( FuncFactory ).GetMethod( "Resolve" ), argsArray );
  var body = Expression.Convert( callEx, returnType );

  var ret = Expression.Lambda( body, param ).Compile();
  return ret;
}

private readonly Container m_Container;
private Type m_Type;

public object Resolve( params object[] args )
{
  return m_Container.Resolve( m_Type, args );
}

但这是不完整的。Resolve() 方法不再是静态的(因为它需要两个实例字段)并且不能被调用。所以这里的问题是

var callEx = Expression.Call( null, typeof( FuncFactory ).GetMethod( "Resolve" ), argsArray );

我认为我需要引用“this”,而不是将 null 作为第一个参数传递。我怎么做?

4

1 回答 1

7

第一个问题是Func<...>不存在的——你需要分别编码到Func<>, Func<,>, Func<,,>, Func<,,,>

现在; 我了解代码,但我不确定您要解决的情况是什么……您能澄清一下吗?可能有更好的选择...

如果它听起来很复杂,那么自定义Expression可能是最合适的选择(比 简单得多Reflection.Emit)。


例如,这有效...

static Delegate MissingFunc(Type result, params Type[] args)
{
    int i = 0;
    var param = Array.ConvertAll(args,
        arg => Expression.Parameter(arg, "arg" + i++));
    var asObj = Array.ConvertAll(param,
        p => Expression.Convert(p, typeof(object)));
    var argsArray = Expression.NewArrayInit(typeof(object), asObj);
    var body = Expression.Convert(Expression.Call(
                null, typeof(Program).GetMethod("Resolve"),
                argsArray), result);
    return Expression.Lambda(body, param).Compile();
}
static void Main()
{
    var func2 = MissingFunc(typeof(string), typeof(int), typeof(float));
}
public static object Resolve( params object[] args) {
    throw new NotImplementedException();
}
于 2009-06-02T14:47:37.283 回答