C # Invokes a generic method through reflection

Source: Internet
Author: User

1. There is a class with a generic method in the class. We're going to call this generic method through reflection.
The namespace is: Transight.HQv4.BLL
Class name is RVCBLL
There are two ways of
A.public List maketest ()
{
return null;
}
B.public list<t> maketest<t> ()
{
return null;
}
2. Reflection, essentially, is the reflection of a namespace string into the object we want.

Example: Assembly bizassembly = Assembly.Load ("Transight.HQv4.BLL");//Reflection namespace var obj = bizassembly.createinstance ("Transigh T.hqv4.bll.rvcbll ", True,

Bindingflags.default, NULL, new Object[]{this. Commonparam}, NULL, NULL)//Create an instance of the class, with an instance of the constructor, equivalent to the Var obj=new rvcbll (this. Commonparam) If you call the normal a method, we can Type test=obj directly.              GetType (); MethodInfo Mi1=test.              GetMethod ("Maketest"); Mi1. Invoke (Obj,null) 3. So what do we do to call the generic method B?
The above reflection steps do not change, to construct a generic method on the basis of MI1
Type Test=obj. GetType ();
MethodInfo Mi1=test. GetMethod ("Maketest");
MethodInfo mi2 = mi1. MakeGenericMethod (test);
Mi1. Invoke (Obj,null)

C # Invokes a generic method through reflection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.