Skip to content

Instantly share code, notes, and snippets.

View jflam's full-sized avatar

John Lam jflam

View GitHub Profile
@jflam
jflam / invoke.cs
Created January 15, 2011 00:17
Dynamic Invocation via DLR
static object InvokeInstanceMethod(object thisParameter, string methodName, Type[] typeArguments, params object[] args) {
CSharpArgumentInfo[] parameterFlags = new CSharpArgumentInfo[args.Length + 1];
Expression[] parameters = new Expression[args.Length + 1];
var thisParameterFlags = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null);
parameterFlags[0] = thisParameterFlags;
parameters[0] = Expression.Constant(thisParameter);
for (int i = 0; i < args.Length; i++) {
parameterFlags[i + 1] = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.Constant | CSharpArgumentInfoFlags.UseCompileTimeType, null);
parameters[i + 1] = Expression.Constant(args[i]);
}