parameterof, propertyof, methodof 05 May 2010


Every C# programmer knows about the keyword typeof, in essence, it returns an instance of System.Type for a given type. It’s used everywhere you have to do reflection on a type known at compilation time. You could achieve pretty much the same with the methods GetType(string) of both Module and Assembly, but the nice thing about typeof is that it’s refactoring proof. Now, every C# programmer also knows that typeof is the only keyword with such abilities. There’s no parameterof, propertyof, methodof, or eventof to access other metadata members. The web is filled with ways to emulate a type safe reflection though. Lately, expression trees helped a lot. This post presents a proof of concept piece of code for a different approach. Considering the following types:
public static class Parameter {

	public static ParameterInfo Of (T value)
	{
		return null;
	}
}

public static class Property {
	
	public static PropertyInfo Of (T value)
	{
		return null;
	}

	public static PropertyInfo Of (MethodInfo method)
	{
		const BindingFlags all = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;

		foreach (var property in method.DeclaringType.GetProperties (all))
			if (method.Equals (property.GetGetMethod (true)))
				return property;

		return null;
	}
}

public static class Method {

	public static MethodInfo Of (Func value)
	{
		return null;
	}

	public static MethodInfo Of (Func value)
	{
		return null;
	}

	public static MethodInfo Of (Action value)
	{
		return null;
	}

	public static MethodInfo Of (Action value)
	{
		return null;
	}
}

They are basically just stubs, but let’s use them anyway:
class Program {

	static void Main (string [] args)
	{
		ParameterInfo parameter = Parameter.Of (args);

		Console.WriteLine ("Parameter of args: {0}", parameter);

		PropertyInfo rank = Property.Of (args.Rank);

		Console.WriteLine ("Property of rank: {0}", rank);

		MethodInfo foo = Method.Of (Foo);

		Console.WriteLine ("Foo method: {0}", foo);

		MethodInfo bar = Method.Of (Bar);

		Console.WriteLine ("Bar method: {0}", bar);
	}

	static int Foo ()
	{
		return 42;
	}

	static void Bar (int b)
	{
		return;
	}
}
Of course if you run the code as is, everything will be null. What I did is that I wrote a little Cecil rewriter that will detect the calls to the different Of methods, and rewrite them to something that will give you the expected results. So for instance, the Parameter.Of call is compiled as:
ldarg.0
call class [mscorlib]System.Reflection.ParameterInfo Parameter::Of(!!0)
But is rewritten in two parts. First we emit at the beginning of the method a way to retrieve of the ParameterInfo of the current method:
ldtoken method void Program::Main(string[])
call class [mscorlib]System.Reflection.MethodBase [mscorlib]System.Reflection.MethodBase::GetMethodFromHandle(valuetype [mscorlib]System.RuntimeMethodHandle)
callvirt instance class [mscorlib]System.Reflection.ParameterInfo[] [mscorlib]System.Reflection.MethodBase::GetParameters()
stloc $reflection_parameters
And we simple replace the Of call with a load from the $reflection_parameters array:
ldloc $reflection_parameters
ldc.i4 0
ldelem.ref
For methods it's a bit different, they are compiled as:
ldnull
ldftn int32 Program::Foo()
newobj instance void class [mscorlib]System.Func`1::.ctor(object,native int)
call class [mscorlib]System.Reflection.MethodInfo Method::Of(class [mscorlib]System.Func`1)
And we rewrite them to remove the creation of a delegate:
ldtoken method int32 Program::Foo()
call class [mscorlib]System.Reflection.MethodBase [mscorlib]System.Reflection.MethodBase::GetMethodFromHandle(valuetype [mscorlib]System.RuntimeMethodHandle)
castclass  [mscorlib]System.Reflection.MethodInfo
For properties, it’s exactly the same, but we have to inject a call to a method that will retrieve the PropertyInfo the get method belongs to. If you’re interested you can read the rewriter code. It’s pretty simple, but is nowhere complete, and I doubt anyone would use it anyway, but it’s still a pretty cool hack.