C ++ / CLI什么类包含全局函数? [英] C++/CLI What class encloses global functions?

查看:89
本文介绍了C ++ / CLI什么类包含全局函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我没记错的话,函数必须是CLR世界中一个类的成员,而全局函数在C ++ / CLI中是可能的。这是否意味着这些全局函数是某种隐藏的全局类的一部分?如果是这样,出于反射目的,如何去获取其类型?

If I remember correctly, functions must be a member of a class in the CLR world, and yet global functions are possible in C++/CLI. Does this mean that these global functions are part of a hidden "global" class of some sort? If so, how would one go about getting its type, for reflection purposes?

推荐答案

是的,.NET元数据支持全局功能,并且变量。在CLR源代码中称为全局类,其名称为< Module> 。使用C#本地语言,它是内部抽象密封为确保您永远无法创建该类的实例,尖括号可帮助避免创建意外的重复类型。

Yes, .NET metadata supports global functions and variables. Called the "Global Class" in CLR source code, its name is <Module>. Using C# vernacular, it is internal, abstract and sealed to ensure you can never create an instance of the class, the angle brackets help to avoid create an accidental duplicate type.

从技术上讲,您可以对此进行反思,您需要BindingFlags.NonPublic和BindingFlags.Static。请记住这是多么不切实际的做法,您需要使用错误的C ++名称来查找任何内容。至少需要链接器.map文件或反汇编程序(如ildasm.exe)才能知道名称。请注意,这很麻烦,对于一个典型的C ++ / CLI项目,很多CRT标识符都会出现在此类中。

Technically you can reflect on it, you'll need BindingFlags.NonPublic and BindingFlags.Static. Do keep in mind how unpractical this is, you need to use the mangled C++ name to find anything back. At least the linker .map file or a disassembler like ildasm.exe is required to know the name. Beware that it is quite messy, lots of CRT identifiers end up in this class for a typical C++/CLI project. Bit of a bug.

最后但并非最不重要的一点是,在< Module> 。除了晦涩的反向排列原因之外,您始终希望在ref类中包含托管代码。尤其是如果您想对此进行反思。更令人担忧的是,太多的本机C ++代码被编译为MSIL。它工作得很好,任何符合C ++ 03的代码都可以编译为MSIL,因此您只能说自己基本上两全其美。严格将C ++ / CLI与本机C ++代码分开非常重要,最容易通过静态库项目完成。

And last but not least, it is almost always a mistake to have code in <Module>. Except for obscure reverse-pinvoke reasons, you always want managed code inside a ref class. Especially so if you want to reflect on it. And more worrisome, getting too much native C++ code compiled to MSIL. It works too well, any C++03 compliant code can be compiled to MSIL so you just can't tell that you are basically get the worst of both worlds. Strictly separating C++/CLI from native C++ code is important, most easily done with a static library project.

这篇关于C ++ / CLI什么类包含全局函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆