我希望有人能为我指明正确的方向,解决以下问题。
我正在开发一个使用 Reflection.Emit 生成类型的项目,在出现需要将 Func<> 传递到新对象的构造函数的需求出现之前,一切都运行良好,如下所示。
public class SearchTerm : IEntity
{
private readonly NavigationProperty<Item> _item;
public SearchTerm()
{
_item = new NavigationProperty<Item>(() => ItemIds);
}
public string[] 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
我遇到的问题是我不确定如何在 IL 中定义委托(delegate)。
我试过以下方法
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);
然后尝试创建委托(delegate)时抛出“不支持的异常”和错误“派生类必须提供实现。”
var funcType = typeof(Func<,>).MakeGenericType(typeBuilder, typeof(IEnumerable<string>));
method.CreateDelegate(funcType);
我也尝试过使用 Delegate.CreateDelegate 和 DynamicMethod,它们都需要在使用它们之前创建类型。
非常感谢任何关于我可能做错的建议。
最佳答案
如果你想调用你的代码,你必须使用 Type
和 MethodInfo
,而不是 TypeBuilder
和 MethodBuilder
s.
因此,您需要 CreateType()
然后使用该 Type
及其方法:
var generatedType = typeBuilder.CreateType();
var funcType = typeof(Func<,>).MakeGenericType(
generatedType, typeof(IEnumerable<string>));
var d = generatedType.GetMethod("func").CreateDelegate(funcType);
关于c# - 使用将 Func<> 作为参数传递的 Reflection Emit 创建构造函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16418904/