How to call the extension method "ElementAt" List <T> with reflection?
I have a problem that after creating an object "oListType01" of type List <MyClass01> and after assigning it to another object "oObjectType" of type "object" I can no longer access the function "ElementAt (1)". I tried to use reflection, but I always get an exception (parameter conflict) in the Call method. Does anyone know why? Milan
MyClass01 oMy1 = new MyClass01();
oMy1._ID = "1";
MyClass01 oMy2 = new MyClass01();
oMy2._ID = "3";
IList<MyClass01> oListType01 = new List<MyClass01>();
oListType01.Add(oMy1);
oListType01.Add(oMy2);
object oObjectType = new object();
oObjectType = oListType01;
From this object, only the oObjectType object is available (up occurs in a separate function call in the real case). VS oObjectType displays two elements that I would like to get for each reflection.
MethodInfo mInfo = typeof(System.Linq.Enumerable).GetMethod("ElementAt").MakeGenericMethod(typeof(object));
object oSingleObject = mInfo.Invoke(oObjectType, new object[] { 1 });
+7
4 answers
, :
- oObjectType - IEnumerable T
.
, , , , , , , , .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Diagnostics;
namespace ConsoleApplication2
{
class MyClass01
{
public String _ID;
public override string ToString()
{
return _ID;
}
}
class Program
{
static void Main(string[] args)
{
MyClass01 oMy1 = new MyClass01();
oMy1._ID = "1";
MyClass01 oMy2 = new MyClass01();
oMy2._ID = "3";
IList<MyClass01> oListType01 = new List<MyClass01>();
oListType01.Add(oMy1);
oListType01.Add(oMy2);
object oObjectType = new object();
oObjectType = oListType01;
Test(oObjectType);
Console.In.ReadLine();
}
private static void Test(object oObjectType)
{
Type tObject = oObjectType.GetType();
Debug.Assert(tObject.IsGenericType);
Debug.Assert(tObject.GetGenericArguments().Length == 1);
Type t = tObject.GetGenericArguments()[0];
Type tIEnumerable = typeof(IEnumerable<>).MakeGenericType(t);
Debug.Assert(tIEnumerable.IsAssignableFrom(tObject));
MethodInfo mElementAt =
typeof(Enumerable)
.GetMethod("ElementAt")
.MakeGenericMethod(t);
Console.Out.WriteLine("o[0] = " +
mElementAt.Invoke(null, new Object[] { oObjectType, 0 }));
Console.Out.WriteLine("o[1] = " +
mElementAt.Invoke(null, new Object[] { oObjectType, 1 }));
}
}
}
0