【发布时间】:2010-10-30 17:03:03
【问题描述】:
我想发出一个返回 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 代表的解决方案。它们必须可转换为确切的绑定类型。
编辑:
我不知道如何更好地描述它......我正在尝试做类似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 作为第一个参数。我该怎么做?
【问题讨论】:
-
我得说我不是 100% 确定我得到了你想要做的事情,但是让 MissingMethod 方法通用不会让你解决问题的一半吗?或者这不是一个选择?
-
哦,另外,我确实知道您必须对其进行多次编码才能涵盖所有可能的 Func 泛型,但正如 Marc 所提到的,无论如何您都必须对其进行硬编码.
-
通过编辑,我仍然不明白规则是什么......你会让它在[Inject]注入什么?为什么?
标签: c# delegates lambda reflection.emit