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

前端 未结 4 1960
清歌不尽
清歌不尽 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:38

    The ElementAt extension method is probably on IEnumerable and so when you treat your list like an object, the extension method won't be available unless you cast it. Either ((List)oObjectType).ElementAt() or (oObjectType as List).ElementAt().

    I have to ask, though, with all due respect why you'd ever want to do this in the first place? It strikes me that there's something wrong here that could be done a little cleaner using interfaces.

提交回复
热议问题