TypeBuilder 上的 TypeBuilder.GetMethod

发布于 2024-10-13 00:41:22 字数 900 浏览 1 评论 0 原文

TypeBuilder.GetMethod 允许您获取由 TypeBuilder 关闭的泛型类型的方法,因此它可以让我执行以下操作:

TypeBuilder tb = ....
// this would throw a NotSupportedException : 
// var ec = typeof(EqualityComparer<>).
//     MakeGenericType(tb).GetMethod("get_Default");

// this works:
var ec = TypeBuilder.GetMethod(tb, typeof(EqualityComparer<>).
    GetMethod("get_Default");

什么不起作用(而且我不能弄清楚如何让它发挥作用)是这样的:

Type collectionOf = typeof(ICollection<>).MakeGenericType(tb);
// throws: 'Type must be a type provided by the runtime. 
// Parameter name: types'
var colEc = TypeBuilder.GetMethod(collectionOf, typeof(EqualityComparer<>).
    GetMethod("get_Default");
// throws NotSupportedException
colEc = typeof(EqualityComparer<>).MakeGenericType(collectionOf).
    GetMethod("get_Default");

有人知道答案吗(我希望是 42)...?

TypeBuilder.GetMethod allows you to get a method on a generic type closed by a TypeBuilder so it lets me to the following:

TypeBuilder tb = ....
// this would throw a NotSupportedException : 
// var ec = typeof(EqualityComparer<>).
//     MakeGenericType(tb).GetMethod("get_Default");

// this works:
var ec = TypeBuilder.GetMethod(tb, typeof(EqualityComparer<>).
    GetMethod("get_Default");

What doesn't work (and I can't figure out how to make it work yet) is this:

Type collectionOf = typeof(ICollection<>).MakeGenericType(tb);
// throws: 'Type must be a type provided by the runtime. 
// Parameter name: types'
var colEc = TypeBuilder.GetMethod(collectionOf, typeof(EqualityComparer<>).
    GetMethod("get_Default");
// throws NotSupportedException
colEc = typeof(EqualityComparer<>).MakeGenericType(collectionOf).
    GetMethod("get_Default");

Anyone know the answer (I hope it's 42)...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟稳重的好男人 2024-10-20 00:41:22

我并不完全清楚你想要做什么(看起来你缺少一些括号,以及其他问题),但是如果你试图获取 MethodInfo EqualityComparer>.get_Default,这对我有用:

var ab = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("test"),
             AssemblyBuilderAccess.Run);
var mb = ab.DefineDynamicModule("test");
var tb = mb.DefineType("TestType");

var ico = typeof(ICollection<>);
var eq = typeof(EqualityComparer<>);

var m = TypeBuilder.GetMethod(eq.MakeGenericType(ico.MakeGenericType(tb)), eq.GetMethod("get_Default"));

It's not completely clear to me what you're trying to do (it looks like you're missing some parentheses, among other issues), but if you're trying to get a MethodInfo for EqualityComparer<ICollection<YourType>>.get_Default, this works for me:

var ab = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("test"),
             AssemblyBuilderAccess.Run);
var mb = ab.DefineDynamicModule("test");
var tb = mb.DefineType("TestType");

var ico = typeof(ICollection<>);
var eq = typeof(EqualityComparer<>);

var m = TypeBuilder.GetMethod(eq.MakeGenericType(ico.MakeGenericType(tb)), eq.GetMethod("get_Default"));
分開簡單 2024-10-20 00:41:22

正如前面所说,您无法对尚未创建的类型进行反射。

您需要跟踪这些方法,或者确保您有一个从调用 TypeBuilder.CreaterType() 返回的运行时类型。 TypeBuilder“方便地”会为您跟踪创建的运行时类型(如果已经创建)。

这适用于 Reflection.Emit 类型中的几乎所有反射操作,而不仅仅是 TypeBuilder

更新:

我之前没有注意到TypeBuilder.GetMethod,所以我上面可能是错的。会详细了解的。 :)

更新 2:可能的解决方案


以下工作有效吗?

typeof(EqualityComparer<>).GetMethod("get_Default").MakeGenericMethod(tb)

应该测试:(

Like said earlier, you cannot do reflection on types that have not been created yet.

You need to either keep track of the methods, or ensure that you have a runtime type that is returned from called TypeBuilder.CreaterType(). TypeBuilder 'conveniently' does keep track of the created runtime type for you if it has already been created.

This goes for almost every reflection operation in Reflection.Emit types, not just TypeBuilder.

Update:

I have not noticed TypeBuilder.GetMethod before, so I am probably wrong above. Will find out the detail. :)

Update 2: Possible solution


Does the following work?

typeof(EqualityComparer<>).GetMethod("get_Default").MakeGenericMethod(tb)

Should be testing :(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文