How does extern work in C #?

Whenever I look deep enough into the reflector, I come across extern methods without a source. I read the msdn documentation at http://msdn.microsoft.com/en-us/library/e59b22c5(v=vs.80).aspx . What I got from this article is that methods with an extern modifier should be introduced. I interpreted this as meaning that it works as an abstract factory pattern. I also noticed that I never saw a non-static extern method. Is a static declaration a requirement (I could see how that makes sense)? I still have a clue, and I'm not sure how this works. It seems to me that the compiler should recognize certain attributes that soften the processing, but I don’t know which attributes are different from those that I encountered, for example, MethodImplAttribute and DllImportAttribute from the MSDN example. How does someone use the extern attribute? He said that in many cases this can increase productivity. Also, how could I search for the source of extern methods like Object.InternalGetEquals() ?

+50
performance c # extern
Feb 24 '11 at 21:30
source share
4 answers

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




When a method declaration includes extern, this method is an external method. external methods are implemented externally, usually using a language other than C #. Because the external method in the declaration has no actual implementation, the body method of the external method simply consists of a semicolon. An external method may not be common. The extern modifier is usually used in conjunction with the DllImport attribute, allowing external methods implemented using DLLs (Dynamic Link Libraries). Execution conditions may support other mechanisms by which implementations of external methods can be provided. When an external method includes the DllImport attribute, the method declaration should also include a static modifier.




How does someone use the extern attribute?

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

How will I look for the source of external methods such as Object.InternalGetEquals ()?

Go to https://github.com/dotnet/coreclr/tree/master/src/vm

+83
Feb 24 2018-11-21T00:
source share

Methods tagged extern with [DllImport] Typically, attributes are called C libraries. This function is useful for invoking WinAPI or legacy code.

This is an example from MSDN:

 using System; using System.Runtime.InteropServices; class MainClass { [DllImport("User32.dll")] public static extern int MessageBox(int h, string m, string c, int type); static int Main() { string myString; Console.Write("Enter your message: "); myString = Console.ReadLine(); return MessageBox(0, myString, "My Message Box", 0); } } 

It calls MessageBox , which is defined inside the Windows library user32.dll . Runtime does all the hard work for you here, although sometimes you need to manually manage memory. If you made a mistake in the signature, your program may fail when called, you may introduce a leak, otherwise the method may return something completely different, so be careful! I find pinvoke.net a great tool to fix signatures for different APIs.

Some extern methods inside the .NET Framework that do not have the [DllImport] attribute but are decorated with [MethodImpl (MethodImplOptions.InternalCall)] , as a rule, are implemented in the CLR itself, which is also written in C. Some of these methods simply do not can be implemented in C #, since they themselves manage the runtime itself, and some are implemented in C, because their performance is critical, and C is faster.

This is what MSDN says about them:

Specifies an internal call. An internal call is a call to a method that is implemented in the runtime of a regular language itself.

Regarding the actual implementation code, I doubt that you can get it from Microsoft, but there are some interesting alternative CLR implementations around so be sure to check them out.

+23
Feb 24 2018-11-21T00:
source share

extern with platform invocation (pinvoke) to facilitate the management of assemblies that cause unmanaged code. The extern keyword informs the compiler of the need to create the right code to ensure proper data marshaling.

+3
Feb 24 2018-11-21T00:
source share

We use the extern modifier in the method declaration. It is used to indicate that the method is implemented externally. The usual use of the extern modifier has the DllImport attribute. With this attribute, calls to functions other than C # are managed. If you are using an extern modifier, you must include the following namespace:

using System.Runtime.InteropServices;

The syntax looks like:

[DllImport("User32.dll")] public static extern int MessageBox(int h, string m, string c, int type);

0
Oct 05 '15 at 11:59 on
source share



All Articles