views:

75

answers:

2

What is a global method in .net? I'm talking about the ones created by the ModuleBuilder.DefineGlobalMethod method.

Can these methods be called from C# or other .net languages?

A: 

Global method means that the method can be called without fully qualifying it's name ie

Method(Param) instead of Module.Method(Param).

In vb a public method inside a module is global.

In c# a public static method in a static class is global.

diamandiev
First of all, in C# (and I think VB too) you can't call public static methods from other classes without qualifying them with the class name. Second and more importantly DefineGlobalMethod is called on the module builder (not a VB module which is a static class), not on a class builder - so the method wouldn't be inside a class.
configurator
You can most definitely call module methods in VB without qualifying the class name. I thought this was the case in C#, but apparently a static class in C# is treated differently than a module in VB.
diamandiev
I stand corrected on the VB front. Are you sure this is what `DefineGlobalMethod` does though? Since the methods still do belong to a VB module, and `DefineGlobalMethod` is directly on the DLL.
configurator
+1  A: 

Hard to talk about this at all in familiar terms, this capability is not at all exposed in the C# or VB.NET languages. The C++/CLI compiler uses it however. The way it is shown in disassemblers like Ildasm.exe or Reflector is also not standardized.

Perhaps the best angle is Reflector, take a look-see at the System.Data.dll assembly. It is in an unnamed namespace ("-" in Reflector), <Module> node. The .cctor you see there is a module initiailizer. Same kind of animal as a static class constructor but at the module level. It runs when the assembly gets loaded. C++/CLI uses it to initialize the C runtime library.

The ___CxxCallUnwindDtor() method you find there is an example of a "global method". The C++/CLI language doesn't give any way to make these kind of functions public, they are always embedded in the metadata with internal accessibility. And can thus not be called directly from a C# or VB.NET program. I haven't played enough with ModuleBuilder to know if that can be messed with at all beyond what C++/CLI does. This is all very obscure and not actually that useful.

Hans Passant
Looking at System.Data has enlightened me. There are some methods there that are defined as public, but there's no way I could find to access them from C#.
configurator