1

Is there any way to find a module initializer in c# via reflection? I'm loading in an assembly with completely unknown types, but there will definitely be a module initializer in the assembly, but there doesn't seem to be any way to find it using reflection. In order for the loaded assembly to function properly, it MUST run its MI. Here' what I'm currently trying:

var asm = Assembly.LoadFrom("GameBase.dll");
var module = asm.Modules.First(); //There's only one module.
//All MI cctor's must be private and static.
var cctor = module.GetMethods(BindingFlags.NonPublic | BindingFlags.Static);

Note that I try to find all methods in the module with those flags because if you inspect the MI with ildasm, it seems like the name gets obfuscated. Unfortunately it does not return any methods.

Using the debugger in Visual Studio, you can see that it's declared as a member in the module, but it's declared as a method in the module, which is probably why it doesn't get return by GetMethods.

How can I find it?

shim
  • 9,289
  • 12
  • 69
  • 108
Mystborn
  • 15
  • 5
  • http://einaregilsson.com/module-initializers-in-csharp/ – pmcilreavy Feb 21 '18 at 04:05
  • @pmcilreavy I know how to create one (which is all that the article describes), I'm trying to find it. – Mystborn Feb 21 '18 at 04:12
  • It says [here](https://blogs.msdn.microsoft.com/junfeng/2005/11/19/module-initializer-a-k-a-module-constructor/) and [here](https://kernel32.net/2016/06/30/dotnet-module-initializers.html) that while .NET supports MIs, C# doesn't support them. – Furkan Kambay Feb 21 '18 at 04:49
  • When you create one and inject it, the program justs injects the compiled IL code. Because C# doesn't support MIs, there is no class for it so there is no method to get it. That is what I understand from it. There are still type initializers (static constructors), though. – Furkan Kambay Feb 21 '18 at 04:53
  • Since a (c)ctor is nothing more than a normal method with special name in terms of metadata, you should be able to find it by its name.You should create a minimal example in IL, compile it and then use `Module.GetMethods()` with all relevant bindingflags to check if it can be found using the c# reflection api. Im not entirely sure if this works but that is what I vaguely remember. – thehennyy Feb 21 '18 at 07:10
  • @FurkanKambay But you can get global methods using Reflection, which also isn't supported by c# – Mystborn Feb 21 '18 at 07:21
  • @thehennyy I mean, I literally put that I was already trying that in the post... – Mystborn Feb 21 '18 at 07:23
  • @Mystborn it seems that you try to get the module cctor from a maybe obfuscated assembly. Test on the green field first, agaist an assembly that does only containt what you want to find. Also there are more BindingFlags than the few you are using, i would expect at least public, nonpublic, static, instance just to be sure to get everything available. – thehennyy Feb 21 '18 at 07:28
  • The module initializer is normally the static constructor (.cctor) of the type named `""`. That type is pretty special, it is the "global class" of an assembly. Reflection in .NET does not return it, you'd have to use the low-level metadata COM interfaces to see it. IMetaDataAssemblyImport, I think. Pretty painful, given that you still know so little, you only know it is there but have no idea whatsoever that it was written the way you demand. Surely there is a better way to check, some side-effect you expect it to have. – Hans Passant Feb 21 '18 at 08:16

0 Answers0