如何调用通过反射一个泛型方法 [英] How to call a generic method through reflection

查看:106
本文介绍了如何调用通过反射一个泛型方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能与反射调用与显式类型参数<的方法; S> 定义结果
例如 oObject.Cast< S>()



是:



 的IList< P> oObject =新的List< P>(); 



我试着用



  oObject.getType()。InvokeMember(铸,BindingFlags.InvokeMethod,空,oObject,NULL)

但它不工作,没有人知道为什么吗?






下面是完整的测试代码但它仍然无法正常工作。最后一行产生总是例外。 ?是否有可能使其工作



 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用的System.Reflection;

命名空间reflection_tester
{
类CBase的
{
公共字符串JA =我的基地;
}

类MyClass01:CBase的
{
公共字符串_ID;

公共字符串ID
{
{返回_ID; }
集合{_ID =价值; }
}
}

类节目
{

公共静态对象包装()
{
/ /返回类MyClass01

的IList℃的名单; MyClass01> lstClass01 =新的List< MyClass01>();

MyClass01 oClass01a =新MyClass01();
oClass01a.ID =1;

MyClass01 oClass01b =新MyClass01();
oClass01b.ID =2;

lstClass01.Add(oClass01a);
lstClass01.Add(oClass01b);

返回lstClass01;
}

静态无效的主要(字串[] args)
{

MyClass01 oMy1 =新MyClass01();
oMy1._ID =1;

MyClass01 oMy2 =新MyClass01();
oMy2._ID =3;

&IList的LT; MyClass01> oListType01 =新的List< MyClass01>();

oListType01.Add(oMy1);
oListType01.Add(oMy2);

对象oObjectType =新的对象();

oObjectType = oListType01;

/ *这个工程* /
IEnumerable的<&CBase的GT; enumList = oListType01.Cast<&CBase的GT;();

MethodInfo的明复= typeof运算(System.Linq.Enumerable).GetMethod(投,新的[] {typeof运算(System.Collections.IEnumerable)})MakeGenericMethod(typeof运算(CBase类))。

/ *这是不行的,为什么呢?抛出异常* /
IEnumerable的<&CBase的GT; enumThroughObject =(IEnumerable的<&CBase的GT;)mInfo.Invoke(oObjectType,NULL);

的回报;
}
}
}


解决方案

的演员扩展方法住在类枚举的,而你需要调用MakeGenericMethod:

  typeof运算(System.Linq的.Enumerable)
.GetMethod(投,新的[] {typeof运算(System.Collections.IEnumerable)})
.MakeGenericMethod(typeof运算(S))
.Invoke(空,新[对象] {oObjectType})

更新:由于该方法是静态的,调用第一个参数是


is it possible to call with reflection a method with "explict type argument" <S> definition
e.g. oObject.Cast<S>() ?

where is:

IList <P> oObject = new List <P>();

I tried with

oObject.getType().InvokeMember( "Cast", BindingFlags.InvokeMethod, null, oObject, null)

but it does not work, does anyone know why?


Here is the complete test code but still it does not work. The last line produce always exception. Is it possible to make it work ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace reflection_tester
{
    class CBase
    {
        public string Ja = "I am the base";
    }

    class MyClass01 : CBase
    {
        public string _ID;

        public string ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
    }

    class Program
    {

        public static object wrapper()
        {
            //return a list of classes MyClass01

            IList<MyClass01> lstClass01 = new List<MyClass01>();

            MyClass01 oClass01a = new MyClass01();
            oClass01a.ID = "1";

            MyClass01 oClass01b = new MyClass01();
            oClass01b.ID = "2";

            lstClass01.Add(oClass01a);
            lstClass01.Add(oClass01b);

            return lstClass01;
        }

        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;

            /* this works */
            IEnumerable<CBase> enumList = oListType01.Cast<CBase>();

            MethodInfo mInfo = typeof(System.Linq.Enumerable).GetMethod("Cast", new[] { typeof(System.Collections.IEnumerable) }).MakeGenericMethod(typeof(CBase));

            /* this does not work, why ? throws exception */
            IEnumerable<CBase> enumThroughObject = (IEnumerable<CBase>)mInfo.Invoke(oObjectType, null);

            return;
        }
    }
}

解决方案

The Cast extension method lives on the class Enumerable, and you need to call MakeGenericMethod:

typeof(System.Linq.Enumerable)
    .GetMethod("Cast", new []{typeof(System.Collections.IEnumerable)})
    .MakeGenericMethod(typeof(S))
    .Invoke(null, new object[] { oObjectType })

update: Because the method is static, the first parameter to Invoke should be null

这篇关于如何调用通过反射一个泛型方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆