Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. LINQ. expressions;
Using System. collections;
Using System. Data. Common;
Using System. Data;
Using System. Data. LINQ;
Using System. reflection;
Using System. diagnostics;
Namespace Consoleapp
{
Public Class Boy
{
Public Int Userid;
Public Int Age;
Public String Name;
Public Static String Move ()
{
Return String . Format ( " The boy begin move; " );
}
Public Static Int Getdoubblenum ( Int Num)
{
Return Num + num;
}
Public Boy (){}
Public String Play ( String Objname)
{
Return " Play " + Objname;
}
}
Class Program
{
Static Void Main ( String [] ARGs)
{
Parameterexpression boyparameter = expression. parameter ( Typeof (Boy ), " Boy " );
Parameterexpression intparameter = expression. parameter ( Typeof (Int32 ), " I " );
Parameterexpression strparameter = expression. parameter ( Typeof ( String ), " Str " );
// Call a static method without Parameters
Methodinfo minfo = Typeof (Boy). getmethod ( " Move " );
Methodcallexpression mcexp = expression. Call ( Null , // The static method is null.
Minfo,
New Expression [] {} // Type of the parameter passed in by the static method: typeof (object)
);
VaR Staticmethodnoprameter = expression. Lambda <func <boy, String >>( Mcexp, boyparameter). Compile ();
// Call a static method with Parameters
Minfo = Typeof (Boy). getmethod ( " Getdoubblenum " );
Mcexp = expression. Call ( Null ,
Minfo,
New Expression [] {intparameter });
VaR Staticmethodhaveprameter = expression. Lambda <func <boy, Int , Int >>( Mcexp, boyparameter, intparameter). Compile ();
// Call common methods
Minfo = Typeof (Boy). getmethod ( " Play " );
Mcexp = expression. Call (boyparameter,
Minfo,
New Expression [] {strparameter });
VaR Generalmethod = expression. Lambda <func <boy, String , String >>( Mcexp, boyparameter, strparameter). Compile ();
Boy boy = New Boy () {userid = 10 , Age = 18 , Name = " Rhythmk " };
Console. writeline (" Staticmethodnoprameter: " + Staticmethodnoprameter (boy ));
Console. writeline ( " Staticmethodhaveprameter: " + Staticmethodhaveprameter (boy, 12 ));
Console. writeline ( " Generalmethod: " + Generalmethod (boy, " Footboy " ));
/* Output result:
* Staticmethodnoprameter: The boy begin move;
* Staticmethodhaveprameter: 24
* Generalmethod: Play the footboy
*/
Console. Read ();
}
}
}