How to call extension method “ElementAt”of List with reflection?

前端 未结 4 1959
清歌不尽
清歌不尽 2020-12-19 07:00

I have problem that after creating object \"oListType01\" of type List < MyClass01 > and after assigning it to the another objet \"oObjectType \" of type \"object\" I ca

4条回答
  •  生来不讨喜
    2020-12-19 07:40

    I will assume you have a valid reason to be doing this but it seems a little wrong. That said here is some code that will accomplish what you are trying to do.

    MethodInfo mInfo = typeof(System.Linq.Enumerable).GetMethod("ElementAt").MakeGenericMethod(typeof(MyClass01));
    object oSingleObject = mInfo.Invoke(oObjectType, new object[] { oObjectType, 1 });
    

    When I run this code I get the second element in the List.

提交回复
热议问题