.NET Framework 在多大程度上依赖于 Windows API?
我知道一些 .NET Framework 库 (FCL) 包装了 Windows API 调用,但我不知道有多少。全部吗?大部分?只有一点点吗?
假设我仅使用托管代码用 C# 编写了一个典型的 winforms 业务应用程序。我的“托管”应用程序中有多少实际上是“非托管”的?
I know some of the .NET Framework library (FCL) wraps Windows API calls, but I don't know how much. All of it? Most of it? Only a little bit?
Let's say I write a typical winforms business application in C# using only managed code. How much of my "managed" application is really "unmanaged" under the covers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Windows 窗体中,几乎所有内容都是托管 Windows API 上的薄包装器。 Windows 窗体控件甚至通过 公开其本机控件句柄处理属性。
话虽如此,例如,如果您使用 WPF,则更多底层逻辑是用托管代码编写的。当然,在某种程度上,事情总是向操作系统进行外壳 - 但大部分框架(WPF、WCF 等)都是用托管代码编写的更高级别的抽象,构建在围绕本机 API 的较低级别包装器之上。
话虽如此,托管代码与本机核心的级别差异很大,具体取决于所讨论的类型。不过,这样做的好处在于,使用 .NET,您实际上不必关心底层实现是本机实现还是托管实现,只需关心它是否是需要通过 IDisposable 或 IDisposable 进行清理的资源即可相似的。
In Windows Forms, nearly everything is a thin wrapper over a managed Windows API. Windows Forms Controls even expose their native control handle via the Handle property.
That being said, if you use WPF, for example, much more of the underlying logic was written in managed code. At some level, of course, things always shell out to the operating system - but much of the framework (WPF, WCF, etc) is higher level abstractions written in managed code, built on top of the lower level wrappers around the native API.
That being said, the level of managed code vs. native core varies greatly, depending on the type in question. The beauty of this, though, is that, with .NET, you really don't have to care about whether the underlying implementation is native or managed, only whether it's a resource which requires cleanup via
IDisposable
or similar.100%。托管只是存在于非托管操作系统之上,而后者当然也存在于非托管硬件上。
我的意思是,我不知道你想如何量化它。公共表面方法?代码行数?全球所有 C# 代码的方法调用总数?但谁真正在乎呢? .NET(WinForms、WPF)中的大多数 UI Windows 代码在某些时候都会通过 Win API。许多其他代码也是如此,例如与文件系统、WCF、ADO.NET 等交互的代码。
这将帮助您解决什么工程问题?
100% of it. Managed is just lives on top of an unmanaged OS, which of course lives on unmanaged hardware.
I mean, I have no idea how you want to quantify it. Public surface methods? Lines of code? Total number of method invocations over all the C# code in the world? Who cares really, though? Most of the UI Windows-y code in .NET (WinForms, WPF) is going go through the Win API at some point. So is a lot of other code such as code that interacts with the file system, WCF, ADO.NET, etc.
What engineering problem is this going to help you solve?
如果您想要一个纯粹的技术答案,那么整个应用程序在某种程度上是不受管理的。但是,既然您是从 .NET 的角度提出问题...
这实际上取决于应用程序的功能。当您使用 winforms 时,应用程序将使用比其他一些类型(例如绘图库)更多的内容。添加一些互联网功能、套接字等,依赖程度就会增加。
不使用 .NET 库的自定义代码越多,由 CLR 管理的百分比就越高。 .NET 中的许多“辅助”函数也是如此。框架中的任何通信代码都将归结为非托管代码。图形库也是如此。如果我想得长一点,我还可以接触到其他一些东西。
我对 Windows 窗体应用程序的猜测。当您爬行堆栈时,某些到大多数代码都是不受管理的。
If you want a purely technical answer, the entire applicatin, at some lower point, is unmanaged. But, since you are asking from a .NET perspective ...
It really depends what the application does. As you are using winforms, the application will use more than some other types (drawing libraries, for example). Add on some Internet capabilities, sockets, etc. and the amount of reliance goes up.
The more custom code you have, not using .NET libraries, the higher percentage is managed by the CLR. the same is true with many of the more "helper" functions in .NET. Any communication code will get down to unmanaged code, in the framework. The same is true for graphics libraries. If I think a bit longer, I can also touch some others.
My guess for a Windows Forms application. Some to most of the code is unmanaged as you crawl the stack.
多年来,.NET 运行时和类库的源代码出现了各种版本。不是每个版本,也不是所有代码,但其中的大部分代码。因此,您可以以此为起点,自己评估受管理和不受管理之间的关系。
http:// /weblogs.asp.net/scottgu/archive/2008/01/16/net-framework-library-source-code-now-available.aspx
我记得看过当 1.0 版本发布时,我查看了 1.0 版本的“参考源”,并对有多少类库(如字符串类、正则表达式类、XML 类等)是用托管 C# 代码编写的感到惊喜。使用操作系统服务(如线程、互斥体、文件 I/O)的最低层类被 P'Invoking 到 Win32 中。但那是很久以前的事了。
There have been various releases of source code to the .NET runtime and class libraries over the years. Not every release and not all of the code, but good chunks of it. So you could start with that and assess the relationship between managed and unmanaged yourself.
http://weblogs.asp.net/scottgu/archive/2008/01/16/net-framework-library-source-code-now-available.aspx
I recall looking at the "reference source" for version 1.0 when that was released and was pleasantly surprised about how much of the class libraries (like the string class, regex class, XML classes, etc...) were written in managed C# code. And the lowest layer of classes that make use of OS services (like threads, mutexes, file i/o) were P'Invoking into Win32. But that was a long time ago.