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



Question:

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 ?


Solution:1

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;  


Solution:2

You can compile Nemerle with Silverlight assembly and then you have Nemerle working on top of Silverlight :)


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »