使用Reflection Emit创建构造函数调用,该函数将Func <>作为参数传递

SCB*_*SCB 5 c# reflection delegates reflection.emit

我希望有人可以指出我正确的方向与以下问题.

我正在开发一个使用Reflection.Emit生成类型的项目,所有这些都一直正常工作,直到我需要将Func <>传递给新对象的构造函数,如下所示.

public class SearchTerm : IEntity
{
   private readonly NavigationProperty<Item> _item;

   public SearchTerm()
   {
       _item = new NavigationProperty<Item>(() => ItemIds);
   }
   public string[] ItemIds { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

使用Linqpad我可以看到IL输出如下:

SearchTerm.<.ctor>b__0:
IL_0000:  ldarg.0     
IL_0001:  call        UserQuery+SearchTerm.get_ItemIds
IL_0006:  stloc.0     // CS$1$0000
IL_0007:  br.s        IL_0009
IL_0009:  ldloc.0     // CS$1$0000
IL_000A:  ret         

SearchTerm..ctor:
IL_0000:  ldnull      
IL_0001:  stloc.0     
IL_0002:  ldarg.0     
IL_0003:  call        System.Object..ctor
IL_0008:  nop         
IL_0009:  nop         
IL_000A:  ldarg.0     
IL_000B:  ldloc.0     
IL_000C:  brtrue.s    IL_001D
IL_000E:  ldarg.0     
IL_000F:  ldftn       UserQuery+SearchTerm.<.ctor>b__0
IL_0015:  newobj      System.Func<System.Collections.Generic.IEnumerable<System.String>>..ctor
IL_001A:  stloc.0     
IL_001B:  br.s        IL_001D
IL_001D:  ldloc.0     
IL_001E:  newobj      UserQuery<UserQuery+Item>+NavigationProperty`1..ctor
IL_0023:  stfld       UserQuery+SearchTerm._item
IL_0028:  nop         
IL_0029:  ret  
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是我不确定如何在IL中定义委托.

我尝试了以下内容

var method = typeBuilder.DefineMethod("func", MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Virtual, typeof(IEnumerable<string>), Type.EmptyTypes);

var methodIl = method.GetILGenerator();
methodIl.Emit(OpCodes.Ldarg_0);
methodIl.Emit(OpCodes.Call, dictionary["get_ItemIds"]);
methodIl.Emit(OpCodes.Ret);
Run Code Online (Sandbox Code Playgroud)

然后尝试创建委托,以下引发"不支持的异常",并显示错误"派生类必须提供实现".

var funcType = typeof(Func<,>).MakeGenericType(typeBuilder, typeof(IEnumerable<string>));
method.CreateDelegate(funcType);
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用Delegate.CreateDelegate和DynamicMethod,它们都需要在使用之前创建类型.

我非常感谢任何有关我可能做错的建议.

svi*_*ick 2

如果你想调用你的代码,你必须使用Types 和MethodInfos,而不是TypeBuilders 和MethodBuilders。

因此,您需要CreateType()然后使用它Type及其方法:

var generatedType = typeBuilder.CreateType();

var funcType = typeof(Func<,>).MakeGenericType(
    generatedType, typeof(IEnumerable<string>));
var d = generatedType.GetMethod("func").CreateDelegate(funcType);
Run Code Online (Sandbox Code Playgroud)