I have a reflection code, to create an instance of List<>
(type parameter known at run-time), and call Add
method to add some values to it. My snippet is something like this:
// here is my type parameter
var genericType = typeof(MyRunTimeType);
// here is a list of my values
MyRunTimeType[] values = MyRunTimeValuesOfTypeMyRunTimeType();
// creating instance of List<>
var listType = typeof(List<>);
var listGenericType = listType.MakeGenericType(genericType);
var listInstance = Activator.CreateInstance(listGenericType);
// getting Add method and call it
var addMethod = listGenericType.GetMethod("Add", genericType);
foreach (var value invalues)
addMethod.Invoke(listInstance, new[] { value });
So, how would you suggest to convert this reflection snippet to an expression-tree?
Aucun commentaire:
Enregistrer un commentaire