tags:

views:

73

answers:

2

System.Reflection does not (AFAIK) support reflecting on global methods in an assembly. At the assembly level, I must start with the root types.

My compiler can produce assemblies with global methods, and my standard bootstrap lib is a dll that includes some global methods. My compiler uses System.Reflection to import assembly metadata at compile time. It seems if I depend on System.Reflection, global methods are not a possibility. The cleanest solution is to convert all of my standard methods to class static methods, but the point is, my language allows global methods, and the CLR supports it, but System.Reflection leaves a gap.

ildasm shows the global methods just fine, but I assume it does not use System.Reflection itself and goes right to the metadata and bytecode.

Besides System.Reflection, is anyone aware of any other 3rd party reflection or disassembly libs that I could make use of (assuming I will eventually release my compiler as free, BSD licensed open source).

SOLVED: There is no gap, except in my knowledge. Thanks for pointing out GetModules, guys!

+6  A: 

Have you looked at Module.GetMethods?

Returns the global methods defined on the module

You can get all the modules of your assembly using Assembly.GetModules().

Jon Skeet
Beat me to it, but I have to admit that I haven't actually tried it. Good question and good answer.
Brian Rasmussen
@Brian: What makes you think I've tried it? ;)
Jon Skeet
@Jon: you probably have some high level power that will let you know if the documentation is accurate or not just by looking at it :)
Brian Rasmussen
Well gosh.. I missed it. Thanks!
mrjoltcola
I have MSDN + a desk full of compiler and .NET reference books, including "Expert .NET 2.0 IL Assembler" and I still miss things like this, embarassing. :|
mrjoltcola
+3  A: 

You keep beating on a gap between the CLR and System.Reflection, but actually, there's no such thing as a global method or a global field.

They are just traditional static methods and static fields that are defined in particular type, named <Module>, which has to be present in every valid assembly.

As Jon said, you can use Module.GetMethod and Module.GetField to operator on the members of the type.

If you want more control, you can just use Mono.Cecil.

Jb Evain
+1 Mono.Cecil is great!
Zach Johnson
I'm not "beating on anything" except my keyboard. I just missed this info, which is why I asked on SO. I learn more everyday. Thanks for the answer. :)
mrjoltcola
@Jb: Regarding Mono.Cecil, and I am aware of it and surely interested in it. The only reason I passed over it was the license. When I release my code, it will be BSD license. I will support building against Mono, but I cannot include a dependency on any tools that aren't common to the official CLR and Mono. Of course, thats all speculative right now, anyway.
mrjoltcola
@mrjoltcola: Mono.Cecil is licensed under the MIT/X11 license (similar to the BSD), so it shouldn't be an issue.
Jb Evain
Oh, I had seen the GPL in a quick scan and missed this. Thank you.
mrjoltcola