如何在C#中的extern工作? [英] How does extern work in C#?

查看:215
本文介绍了如何在C#中的extern工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我看够深成反射我碰到的extern 方法,没有来源。我读的MSDN文档的<一个href="http://msdn.microsoft.com/en-us/library/e59b22c5(v=vs.80).aspx">http://msdn.microsoft.com/en-us/library/e59b22c5(v=vs.80).aspx.我从这篇文章得到的是,与的extern 修改方法必须注射。我PTED这间$ P $意味着它的工作原理有点像一个抽象工厂模式。我也注意到,我从来没有见过一个非静态外部方法。是静态声明的要求(我可以看到这是如何有意义)?我仍然猜测这里,我不知道它是如何工作。在我看来,像编译器必须认识到,减轻处理某些属性,但我不知道是什么的属性以外的人我遇到像 MethodImplAttribute DllImportAttribute 从MSDN的例子。人应该如何利用的extern 属性?它说,在许多情况下,这可以提高性能。另外,我怎么会去寻找到的extern源 Object.InternalGetEquals方法()

解决方案

考虑阅读C#的规范,它回答了许多问题的第10.6.7。它我复制的一部分在这里为您的方便:


  

在方法声明中包含   extern修饰符,则称该方法为   是一种外部方法。外部   方法是在外部实现的,   通常使用以外的语言   C#。因为外部方法   声明不提供任何实际的   实现中,方法-体的   外部方法简单地由一个   分号。外部方法可能不   是通用的。该extern修饰符是   通常配合使用一个   DllImport属性,   允许外部方法是   通过DLL(动态链接实施   库)。执行环境   可以支持其他机制,即   外部方法的实现   可以提供。当外部   方法包括一个DllImport属性,   方法声明还必须   包括static修饰符。


  

人应该如何利用EXTERN属性?

  • 写您的code在您所选择的非托管语言。
  • 在编译成一个DLL,导出你的code入口点。
  • 请一个互操作的库,该方法定义为在给定的DLL一个外部的方法。
  • 从C#调用它。
  • 利润!
  

我怎么会去寻找到的extern方法的源像Object.InternalGetEquals()?

  • 找到一份工作,在微软。
  • 在获得许可的CLR源$ C ​​$ C程序存储库进行征用。这样做。
  • 查找名为虚拟机的目录,大部分的东西就在那里。

Whenever I look deeply enough into reflector I bump into extern methods with no source. I read the msdn documentation at http://msdn.microsoft.com/en-us/library/e59b22c5(v=vs.80).aspx. What I got from that article is that methods with the extern modifier have to be injected. I interpreted this to mean it works something like an abstract factory pattern. I also noticed that I've never seen a non-static extern method. Is static declaration a requirement (I could see how this would make sense)? I'm still guessing here and I'm not sure how it actually works. It seems to me like the compiler must recognize certain attributes that mitigate processing, but I don't know what the attributes are other than ones I've come across like MethodImplAttribute and DllImportAttribute from the MSDN example. How does someone leverage the extern attribute? It said that in many instances this can increase performance. Also, how would I go about looking into the source of extern methods like Object.InternalGetEquals()?

解决方案

Consider reading section 10.6.7 of the C# specification, which answers many of your questions. I reproduce part of it here for your convenience:


When a method declaration includes an extern modifier, that method is said to be an external method. External methods are implemented externally, typically using a language other than C#. Because an external method declaration provides no actual implementation, the method-body of an external method simply consists of a semicolon. An external method may not be generic. The extern modifier is typically used in conjunction with a DllImport attribute, allowing external methods to be implemented by DLLs (Dynamic Link Libraries). The execution environment may support other mechanisms whereby implementations of external methods can be provided. When an external method includes a DllImport attribute, the method declaration must also include a static modifier.


How does someone leverage the extern attribute?

  • Write your code in the unmanaged language of your choice.
  • Compile it into a DLL, exporting the entry point of your code.
  • Make an interop library that defines the method as an extern method in the given DLL.
  • Call it from C#.
  • Profit!

How would I go about looking into the source of extern methods like Object.InternalGetEquals()?

  • Get a job at Microsoft.
  • Obtain permission to enlist in the CLR source code respository. Do so.
  • Look in the directory called "vm", most of that stuff is in there.

这篇关于如何在C#中的extern工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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