且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用非托管代码获取托管信息

更新时间:2021-11-02 02:59:00

不能使用System::Reflection可以通过一个简单的原因来挖掘非托管代码:它没有反射使用的元数据.

如果您了解.NET的工作原理,则可以看到编译器(和IDE)可以查看"引用程序集的所有元数据,并可以确保在编译过程中所有外部"类型及其成员的正确链接.在C ++中,根本没有这样的东西.创建了针对单模块编程的传统语言,并基于过时的坏脏#include(头文件)和目标文件引入了模块化编程.

实际上,许多使用非托管平台的编程系统的元数据都与多年前的.NET元数据类似:Ada,Turbo Pascal,Object Pascal,Delphi,Modula等. .NET的主要区别在于:旧的元数据完全依赖于实现(不同制造商的Ada编程系统具有不兼容的元数据结构和格式),而且重要的是,元数据未包含在可执行模块中, NET程序集一样.这些系统的元数据在.NET中工作得差不多,但是必须像C ++目标文件或库那样单独提供.元数据可以被认为是对象文件系统的扩展,而不是可执行模块.

如果您仔细研究C ++,C ++/CLI混合模式(托管+非托管)开发,甚至在P/Invoke上,都可以轻松地发现元数据信息是通过实际的源代码(混合模式的情况)还是手动"传递的,通过基于已有知识编写方法的方法签名.你为什么这么认为?因为缺少元数据.这就是为什么不能使Reflection能够看到"非托管代码声明的确切原因.

—SA
You cannot use System::Reflection to dig into unmanaged code by one simple reason: it does not have metadata used by Reflection.

If you understand how .NET works, you can see that the compiler (and the IDE) can "see" all the metadata of referenced assemblies and can ensure correct linkage of all the "external" types and their members during compilation. In C++, there is no such things at all. Legacy languages was created oriented to single-module programming, and modular programming was introduced based on archaic bad dirty #include (header files) and object files.

In fact, many programming systems working with unmanaged platforms, had metadata similar to the .NET metadata many years ago: Ada, Turbo Pascal, Object Pascal, Delp Modula, and a lot more. The major difference with .NET is: that old metadata was totally implementation-depended (Ada programming systems by different manufacturers had incompatible metadata structures and formats), and, importantly, metadata was not included in executable modules, as it is done with NET assemblies. The metadata of those systems worked pretty much line in .NET, but it had to be supplied separately, as with C++ object files or libraries; that metadata could be considered as extension of object-file system, not executable modules.

If you look thoroughly at C++, C++/CLI mixed mode (managed + unmanaged) development and even at P/Invoke, you can easily figure out that metadata information is passed either through actual source code (mixed-mode case) or "manually", by writing method signatures of method based on some preexisting knowledge. Why do you think? Because the lack of metadata. And this is the exact reason why it''s not possible to make Reflection capable of "seeing" the declaration of unmanaged code.

—SA