Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用传递Func的反射Emit创建构造函数调用<&燃气轮机;作为参数_C#_Reflection_Delegates_Reflection.emit - Fatal编程技术网

C# 使用传递Func的反射Emit创建构造函数调用<&燃气轮机;作为参数

C# 使用传递Func的反射Emit创建构造函数调用<&燃气轮机;作为参数,c#,reflection,delegates,reflection.emit,C#,Reflection,Delegates,Reflection.emit,我希望有人能为我指出以下问题的正确方向 我正在处理一个项目,其中类型是使用Reflection.Emit生成的,在出现需要将Func传递给新对象的构造函数的需求之前,所有这些都工作得很好,如下所示 public class SearchTerm : IEntity { private readonly NavigationProperty<Item> _item; public SearchTerm() { _item = new Navigatio

我希望有人能为我指出以下问题的正确方向

我正在处理一个项目,其中类型是使用Reflection.Emit生成的,在出现需要将Func传递给新对象的构造函数的需求之前,所有这些都工作得很好,如下所示

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

   public SearchTerm()
   {
       _item = new NavigationProperty<Item>(() => ItemIds);
   }
   public string[] ItemIds { get; set; }
}
公共类搜索术语:IEntity
{
私有只读导航属性_项;
公共搜索术语()
{
_item=新的NavigationProperty(()=>ItemId);
}
公共字符串[]ItemIds{get;set;}
}
使用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  
SearchTerm.b\u 0:
IL_0000:ldarg.0
IL_0001:调用UserQuery+SearchTerm.get_ItemId
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:调用System.Object..ctor
IL_0008:没有
IL_0009:没有
IL_000A:ldarg.0
IL_000B:ldloc.0
IL_000C:brtrue.s IL_001D
IL_000E:ldarg.0
IL\u 000F:ldftn UserQuery+SearchTerm.b\u 0
IL_0015:newobj系统函数
IL_001A:stloc.0
ILU 001B:br.s ILU 001D
IL_001D:ldloc.0
IL_001E:newobj UserQuery+NavigationProperty`1
IL\u 0023:stfld UserQuery+SearchTerm.\u项
IL_0028:没有
IL_0029:ret
我的问题是,我不确定如何在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);
var method=typeBuilder.DefineMethod(“func”,MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Virtual,typeof(IEnumerable),Type.EmptyTypes);
var methodIl=method.GetILGenerator();
methodIl.Emit(操作码Ldarg_0);
Emit(opcode.Call,dictionary[“get_ItemIds]”);
发射方法(操作码Ret);
然后尝试创建委托时,以下抛出“不支持的异常”,错误为“派生类必须提供实现”

var funcType=typeof(Func).MakeGenericType(typeBuilder,typeof(IEnumerable));
方法CreateDelegate(funcType);
我还尝试使用Delegate.CreateDelegate和DynamicMethod,这两种方法都要求在使用它们之前创建类型


如果您想调用代码,您必须使用
Type
s和
MethodInfo
s,而不是
TypeBuilder
s和
MethodBuilder
s

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

var generatedType = typeBuilder.CreateType();

var funcType = typeof(Func<,>).MakeGenericType(
    generatedType, typeof(IEnumerable<string>));
var d = generatedType.GetMethod("func").CreateDelegate(funcType);
var generatedType=typeBuilder.CreateType();
var funcType=typeof(Func).MakeGenericType(
generatedType,typeof(IEnumerable));
var d=generatedType.GetMethod(“func”).CreateDelegate(funcType);

所以在获得实际类型之前不能创建代理?只需确认当前使用typebuilder生成的类SearchTerm将无法包含_item=new NavigationProperty(()=>ItemId);因为我需要调用createType来使用委托中类的属性。顺便说一句,谢谢你的邀请response@SCB我不明白为什么需要创建委托的实例来生成代码。您需要生成代码来创建委托,而不仅仅是创建委托。好的,但是我的SearchTerm构造函数创建了一个新的NavigationProperty,我需要将一些内容传递到对象构造函数调用中。@SCB是的,但您需要生成这样做的代码,您不需要实际调用
CreateDelegate()
。你应该做的:1。创建“匿名”方法(实际上是一个普通方法,IL中没有匿名方法)。2.生成使用
ldftn yourMethod
newobj Func
创建代理的代码。
var generatedType = typeBuilder.CreateType();

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