Solution to invoke the Paradigm method with the type passed in

Source: Internet
Author: User

Cause: Customizing a GridView control whose data source comes from a common method get<t> (), according to your incoming T to the database to get the corresponding data, the problem is to define the GridView control can not be set on the interface side of the use of generics, Data can only be bound at each time that the GridView control is used. It doesn't matter if you don't understand the cause, we use a piece of code to describe the problem:
I would like to use the type TT from the outside to invoke the Test1 paradigm method.

Class Program
{
static void Main (string[] args)
{
MyClass m = new MyClass ();
M.tt = typeof (program);
M.test2 ();
}


}

Class MyClass
{
Public Type TT {get; set;}
public int UserID {get; set;}
public string Name {get; set;}
public string Test2 ()
{
Test1 <T> ();
I would like to use the type TT from the outside to invoke the Test1 paradigm method.
}
public string Test1 <T> ()
{
Return typeof (T). ToString ();
}

}

Solution:

Class MyClass
{
Public Type TT {get; set;}
public int UserID {get; set;}
public string Name {get; set;}
public string Test2 ()
{
Object result = typeof (MyClass). GetMethod ("Test1").
MakeGenericMethod (TT). Invoke (this, null);
return result. ToString ();
}
public string test1<t> ()
{
Return typeof (T). ToString ();
}

}Content sharing from: http://www.cnblogs.com/nuaalfm/archive/2009/02/24/1397340.html Thank you.

Solution to invoke the Paradigm method with the type passed in

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.