I am generating a List<T> with a runtime-determined type parameter. I'd like to invoke the ForEach method to iterate over the items in the list:
//Get the type of the list elements
Type elementType = GetListElementType(finfo);
Type listType = Type.GetType("System.Collections.Generic.List`1["
+ elementType.FullName + "], mscorlib", true);
//Get the list
var list = getList.Invoke(null, new Object[] { finfo.GetValue(myObject) });
MethodInfo listForEach = listType.GetMethod("ForEach");
//How do I do this? Specifically, what takes the place of 'x'?
listForEach.Invoke(list, new object[] { delegate ( x element )
{
//operate on x using reflection
}
});
Given a MethodInfo corresponding to the ForEach method contained in my runtime-generated list type, what's the proper way to invoke it using an anonymous method? The above is my first stab, but don't know how to declare the type of the anonymous method's parameter.