简体   繁体   English

CLR如何处理C#中的外部方法调用

[英]How CLR handles extern method calls in C#

I want to ask why all extern method calls are static? 我想问为什么所有外部方法调用都是静态的? How the CLR handles these calls? CLR如何处理这些呼叫?

Extern method calls are to unmanaged code. 外部方法调用针对非托管代码。 As such, it doesn't make sense to be called on a (managed) object instance - the first (hidden) argument in an instance method is the instance reference, aka this . 因此,在(托管)对象实例上调用没有意义-实例方法中的第一个(隐藏)参数是实例引用,也就是this Typically, extern methods just involve simple types (primitives, string, etc) - not objects (except perhaps arrays - and even they are often resolved to IntPtr first). 通常, extern方法只涉及简单类型(基元,字符串等),而不涉及对象(也许是数组除外),甚至它们通常通常首先解析为IntPtr

extern calls also must generally conform to a "C-style" API, and C doesn't know anything about objects, thus the calls are static . 通常, extern调用也必须符合“ C风格” API,并且C对对象一无所知,因此这些调用是static

My statement isn't 100% true as there is a ThisCall calling convention which can be used with [DllImport] as an aid in calling C++ methods. 我的陈述不是100%正确,因为有一个ThisCall调用约定,可以与[DllImport]一起使用来辅助调用C ++方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM