viernes, diciembre 31, 2004

ReflectionOnly apis

Since I want to end this year with a good thing, I will talk about a new mini api contained in the 2.0 version of .Net, called ReflectionOnly. As its title says, this is a group of methods that load assemblies and retrieve the metainformation. The difference with the Load methods, is that the last retrieve information and also prepare the environment to execute code contained withint them (late binding is something that uses this feature).

Since there can be time when it is important to only load the metainformation -based on performance, for example-, ReflectionOnly methods help that.

Probably we will include this in Mono in the next weeks, as long as I finish the methods -currently I have a working Assembly.ReflectionOnlyLoadFrom method- and let them to be reviewed by the jit guys.

Below an example:


using System;
using System.Reflection;

public class Tester {

static void Main ()
{
Assembly ass1, ass2;

AppDomain.CurrentDomain.AssemblyLoad += OnAssemblyLoad;

ass1 = Assembly.ReflectionOnlyLoadFrom ("AssemblyRefParent.dll");
ass2 = Assembly.ReflectionOnlyLoadFrom ("AssemblyRefChild.dll");

Type t = ass2.GetType ("Child");
if (t == null) {
Console.WriteLine ("Type Child could not be loaded");
return;
}

Console.WriteLine ("Parent of Child = {0}", t.BaseType.Name);

Console.WriteLine ("ReflectionOnly Assemblies:");
foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies ()) {
Console.WriteLine ("\t{0}, ReflectionOnly = {1}", a.FullName, a.ReflectionOnly);
}

}

static void OnAssemblyLoad (object o, AssemblyLoadEventArgs args)
{
Assembly loaded = args.LoadedAssembly;

Console.WriteLine ("Assembly Loaded = {0}", loaded.FullName);
Console.WriteLine ("Is Reflection Only = {0}\n", loaded.ReflectionOnly);
}

}

Output

Assembly Loaded = AssemblyRefParent, Culture=neutral
Is Reflection Only = True

Assembly Loaded = AssemblyRefChild, Culture=neutral
Is Reflection Only = True

Parent of Child = Parent
ReflectionOnly Assemblies:
AssemblyRefDriver, Culture=neutral, ReflectionOnly = False
mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, ReflectionOnly = False
AssemblyRefChild, Culture=neutral, ReflectionOnly = True
AssemblyRefParent, Culture=neutral, ReflectionOnly = True

5 comentarios:

Roberto Iza Valdés dijo...
Este blog ha sido eliminado por un administrador de blog.
Roberto Iza Valdés dijo...
Este blog ha sido eliminado por un administrador de blog.
Roberto Iza Valdés dijo...
Este blog ha sido eliminado por un administrador de blog.
Roberto Iza Valdés dijo...
Este blog ha sido eliminado por un administrador de blog.
Unknown dijo...
Este comentario ha sido eliminado por el autor.