Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting types in mscorlib 2.0.5.0 (aka Silverlight mscorlib) via reflection on?

I am trying to add Silverlight support to my favorite programming langauge Nemerle.

Nemerle , on compilation procedure, loads all types via reflection mainly in 2 steps

1-) Uses Assembly.LoadFrom to load assembly 2-) Usese Assembly.GetTypes() to get the types

Then at the end of compilation it emits the resolved types with Reflection.Emit.

This procedure works for all assemblies including Silverlight ones except mscorlib of silverlight.

In c# this fails:

 var a = System.Reflection.Assembly.LoadFrom(@"c:\mscorlib.dll");

but this passes :

var a = System.Reflection.Assembly.ReflectionOnlyLoadFrom(@"c:\mscorlib.dll"); 

Bu in the latter , a.GetTypes() throws an exception sayin System.Object's parent does not exist.

Is there a way out ?

like image 850
user67754 Avatar asked Apr 16 '09 06:04

user67754


1 Answers

Assuming you're trying to reflect over Silverlight's mscorlib from the standard CLR, this won't work because the CLR doesn't permit loading multiple versions of mscorlib. (Perhaps this is because it could upset resolution of its core types).

A workaround is to use Mono.Cecil to inspect the types: http://mono-project.com/Cecil. This library actually performs better than .NET's Reflection and is supposed to be more powerful.

Here's some code to get you started:

AssemblyDefinition asm = AssemblyFactory.GetAssembly(@"C:\mscorlib.dll");

var types =
    from ModuleDefinition m in asm.Modules
    from TypeDefinition t in m.Types
    select t.Name;
like image 100
Joe Albahari Avatar answered Sep 28 '22 06:09

Joe Albahari