Dynamic calling method using emit (Technical Prototype 2)

Source: Internet
Author: User
Tags emit

After studying it, I found that the system already has such functions. Dizzy !! Implement a method similar to func and action.
Call example:
Object inst = new program ();
Decimal ret = Inst. Dynamic <guid, Int, decimal> ("helloworld", guid. newguid (), 2009 );
The source code is as follows:

  Using  System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. reflection. emit;
Using System. reflection;
Using Evlon. util. Dynamic;

Namespace Consoleapplication2008
{
Public Class Program
{
Static Void Main ( String [] ARGs)
{
Object Inst = New Program ();
Decimal RET = Inst. Dynamic < Guid, Int , Decimal > ( " Helloworld " , Guid. newguid (), 2009 );
Console. writeline (RET );

Console. Readline ();
}

Public Decimal Helloworld (guid userid, Int Year)
{
Return Userid. gethashcode () + Year;
}
}

}
Namespace Evlon. util. Dynamic
{
Public Static Class Typeextension
{
Public Static Tresult dynamic < T, tresult > ( This Object Inst, String Methodname, t Arg)
{
Return (Tresult) Delegate. createdelegate ( Typeof (Func < T, tresult > ), Inst, methodname). dynamicinvoke (ARG );
}
Public Static Tresult dynamic < T1, T2, tresult > ( This Object Inst, String Methodname, T1 arg1, T2 arg2)
{
Return (Tresult) Delegate. createdelegate ( Typeof (Func < T1, T2, tresult > ), Inst, methodname). dynamicinvoke ( New Object [] {Arg1, arg2 });
}
Public Static Tresult dynamic < T1, T2, T3, tresult > ( This Object Inst, String Methodname, T1 arg1, T2 arg2, T3 arg3)
{
Return (Tresult) Delegate. createdelegate ( Typeof (Func < T1, T2, T3, tresult > ), Inst, methodname). dynamicinvoke ( New Object [] {Arg1, arg2, arg3 });
}
Public Static Tresult dynamic < T1, T2, T3, T4, tresult > ( This Object Inst, String Methodname, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
Return (Tresult) Delegate. createdelegate ( Typeof (Func < T1, T2, T3, T4, tresult > ), Inst, methodname). dynamicinvoke ( New Object [] {Arg1, arg2, arg3, arg4 });
}

Public Static Void Dynamic < T > ( This Object Inst, String Methodname, t Arg)
{
Delegate. createdelegate ( Typeof (Action < T > ), Inst, methodname). dynamicinvoke (ARG );
}
Public Static Void Dynamic < T1, T2 > ( This Object Inst, String Methodname, T1 arg1, T2 arg2)
{
Delegate. createdelegate ( Typeof (Action < T1, T2 > ), Inst, methodname). dynamicinvoke ( New Object [] {Arg1, arg2 });
}
Public Static Void Dynamic < T1, T2, T3 > ( This Object Inst, String Methodname, T1 arg1, T2 arg2, T3 arg3)
{
Delegate. createdelegate ( Typeof (Action < T1, T2, T3 > ), Inst, methodname). dynamicinvoke ( New Object [] {Arg1, arg2, arg3 });
}
Public Static Void Dynamic < T1, T2, T3, T4 > ( This Object Inst, String Methodname, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
Delegate. createdelegate ( Typeof (Action < T1, T2, T3, T4 > ), Inst, methodname). dynamicinvoke ( New Object [] {Arg1, arg2, arg3, arg4 });
}


}
}

 

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.