Common Expression Methods

Source: Internet
Author: User
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 ();
}



}

}

 

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.