C#通過反射調用泛型方法

來源:互聯網
上載者:User

標籤:ar   sp   div   on   bs   ad   ef   new   as   

1.有一個類,類中有一個泛型方法。我們要通過反射來調用這個泛型方法。
  命名空間為:Transight.HQv4.BLL
  類名為RVCBLL
  有兩個方法
  a.public List makeTest()
        {
            return null;
        }
  b.public List<T> makeTest<T>()
        {
            return null;
        }
2.反射,實質就是把命名空間的字串反射成我們想要的對象

   例:Assembly bizAssembly = Assembly.Load("Transight.HQv4.BLL");//反射命名空間       var obj = bizAssembly.CreateInstance("Transight.HQv4.BLL.RVCBLL", true,

         BindingFlags.Default, null, new object[]{this.CommonParam}, null, null)//建立類執行個體,帶建構函式的執行個體,相當於var obj=new RVCBLL(this.CommonParam)              若是調用普通的a方法,我們直接這樣就可以                Type test=obj.GetType();              MethodInfo mi1=test.GetMethod("makeTest");              mi1.Invoke(obj,null)3.那我們要調用泛型方法b要怎麼做呢?
  以上的反射步驟不變,要在mi1的基礎上構造泛型方法
     Type test=obj.GetType();
     MethodInfo mi1=test.GetMethod("makeTest");
     MethodInfo mi2 = mi1.MakeGenericMethod(test);
     mi1.Invoke(obj,null)

C#通過反射調用泛型方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.