如何使用 Reflection.Emit 创建嵌套类型?

发布于 2025-01-02 03:01:46 字数 824 浏览 1 评论 0原文

我有一个非常复杂的需求来使用 Reflection Emit 包创建一个类。该类是一个简单的 DTO,将与 WCF 一起使用来定义服务。很简单,真的。但是,我必须使用嵌套类型。

我目前的测试不起作用。我可以创建主类型,但无法创建嵌套类型。

var serviceClassBuilder = _module.DefineType("something", TypeAttributes.Public | TypeAttributes.Class);

// Create a nested class that will hold the input definition
var nested_type_builder = serviceClassBuilder.DefineNestedType(className,
    TypeAttributes.NestedPublic | TypeAttributes.Class |
    TypeAttributes.SequentialLayout);

serviceClassBuilder.CreateType(); // <-- NOT WHAT I EXPECTED!

创建的类中没有任何嵌套类型。是否可以?这些课程就像上面一样简单。我已经阅读了上面 Emit 包的一些限制,但我不确定它是否适用于我使用的这样一个简单的示例。

我什至尝试在创建类型之前添加以下行:

nested_type_builder.CreateType();

尽管我认为第一个构建器应该完成 CreateType 的级联执行,毕竟我不需要调用任何方法来创建托管类型的程序集, ETC。

I have a very complex need to create a class using Reflection Emit package. The class is a simple DTO and will be used with WCF to define services. Simple, really. However, I must use a nested type.

My current tests doesn't work. I can create the main type but I'm unable to create the nested type.

var serviceClassBuilder = _module.DefineType("something", TypeAttributes.Public | TypeAttributes.Class);

// Create a nested class that will hold the input definition
var nested_type_builder = serviceClassBuilder.DefineNestedType(className,
    TypeAttributes.NestedPublic | TypeAttributes.Class |
    TypeAttributes.SequentialLayout);

serviceClassBuilder.CreateType(); // <-- NOT WHAT I EXPECTED!

There aren't any nested types in the class created. Is it possible? The classes are as simple as above. I've read above some limitations of the Emit package, but I'm not sure it applies to such a simple example as the one I used.

I even tried adding the line below before the create type:

nested_type_builder.CreateType();

Even though I think that the first builder should have done a cascade execution of CreateType, after all, I don't need to call anything to create methods, the assembly that hosts the type, etc.

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

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

发布评论

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

评论(1

゛清羽墨安 2025-01-09 03:01:46

首先,我认为你必须调用nested_type_builder.CreateType(),否则你会得到一个异常。

但如果我这么做了,它的行为就会很奇怪。从 serviceClassBuilder.CreateType() 返回的 Type 似乎不包含任何嵌套类型,至少如果您使用 GetNestedTypes() 查看它们>。 但是如果您实际将程序集保存到磁盘并查看它,它会完全包含应有的嵌套类型。

因此,如果您只需要使用生成的程序集文件,那么应该没问题。我认为 TypeBuilder 返回的类型的 GetNestedTypes() 的行为是一个错误。

First, I think you have to call nested_type_builder.CreateType(), or you'll get an exception.

But if I did that, it behaved strangely. The Type returned from serviceClassBuilder.CreateType() doesn't seem to contain any nested types, at least if you look at them using GetNestedTypes(). But if you actually save the assembly to the disk and look at it, it contains the nested type exactly as it should.

So, if you only need to work with the generated assembly file, you should be fine. I think the behavior of GetNestedTypes() of the type returned by TypeBuilder is a bug.

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