Archive for the ‘Mirror Mirror’ Category

MirrorMirror: Working on the generics API

June 12, 2010 2 comments

Yesterday I finished the basic invokation API for MirrorMirror. Now I want it to start supporting generic methods. Since the current API uses params object[] for the parameters, I don’t know how to add the type parameters to that. I want to avoid the usage of generics in the API, because several times you will be dealing with internal types, so you can’t add them as a type parameter.

One idea I have is to change the current, simple API:

var result = object.Invoke("Method", arg1, arg2);


var result = object.Member("Method").Invoke(arg1, arg2);

so I can add method to that chain to support generics:

var result = object.Member("Method").Generic( type1, type2).Invoke(arg1, arg2);

Following this schema, I can always give support to the first API, but it will only work for non generic methods.
Also I would like to add support to generic types inference, so if you have a method with this signature:

private string MyGenericMethod<T1,T2>( T1 a, T2 b)

you can call it without specify the type parameters.

I will be working on this in the following days and will post the final thoughts.

MirrorMirror: Methods support/Api Improvements

June 3, 2010 Leave a comment

MirrorMirror now supports methods! The only restriction is the method can’t have more than 8 or 9 parameters (for void methods). This is because of the DynamicMethod working as a delegate and the need to have each Action and Func signature. Anyway, who needs 10 arguments in a method?

Also, I improved the API so you have only 3 main methods:

  • Get: Retrieves the value of the given fields and properties chain
  • Set: Sets the values of the field or property with the given name
  • Invoke: Ivokes the method with the given name that matches the given arguments
var name = person.Get<string>("Name");
person.Set("Name", "Ivo Wiblo");

var name = person.Get<string>("_name");
person.Set("_name", "Ivo Wiblo");

var name = person.Invoke<string>("GetName");
person.Invoke("SetName", "Ivo Wiblo");

var countryName = address.Get("City._country.Name");

Mirror, Mirror on the wall

June 1, 2010 Leave a comment

It has been a long time since my last post. I moved to a new place and that keeped me busy these days (I even left Magiq for a while).

Well, I’m here to present you a new project I was working on. In fact, the source code was written inside of Magiq but I needed it for another project, so I decided to put it in a new project so I (and everyone) could use it easily. This is Mirror Mirror.

Basically, Mirror Mirror is a wrapper for IL operations related to accessing by reflection to objects members. The API is easier than regular reflection and it uses IL for generating delegates so it is several times faster.

For example, instead of

var name = typeof(Person).GetField("name", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(person);

you can do

var name = person.Property<string>("Name");

You can take a look at it in

Categories: C#, Mirror Mirror Tags: , ,