An interesting problem. There are a few things you can do first. If you only need a delegate for the installer, you can use Delegate.CreateDelegate .
Delegate.CreateDelegate(typeof(Action<Foo, string>),typeof(Foo).GetProperty("A").GetSetMethod()) as Action<Foo,String>;
If you use .NET 3.5+, you can use expression trees, and I highly recommend exploring them with DynamicMethod, they have limited use in .NET 3.5 and almost no .NET 4. (TypeBuilder is still very useful).
var targetExpression = Expression.Parameter(typeof(Foo),"target"); var valueExpression = Expression.Parameter(typeof(string),"value"); var expression = Expression.Lambda<Action<Foo,string>>( Expression.Call( targetExpression, typeof(Foo).GetProperty("A").GetSetMethod(), valueExpression ), targetExpression, valueExpression );
In .NET 4, you can write it a little prettier using Expression.Assign , but it is not much better.
Finally, if you really want to do this in IL, this will work.
DynamicMethod method = new DynamicMethod("Setter", typeof(void), new[] { typeof(Foo), typeof(string) }, true); var ilgen = method.GetILGenerator(); ilgen.Emit(OpCodes.Ldarg_0); ilgen.Emit(OpCodes.Ldarg_1); ilgen.Emit(OpCodes.Callvirt, typeof(Foo).GetProperty("A").GetSetMethod()); ilgen.Emit(OpCodes.Ret); var action = method.CreateDelegate(typeof(Action<Foo,string>)) as Action<Foo,string>;
I think your problem is that you are currently calling the set method actually getMethod, the error on this line is: MethodInfo setMethod = typeof(Foo).GetMethod("get_" + propName);
I have never assigned parameter attributes, but this can also be a problem.