Emit learning-basics-add attributes, constructors, and methods for dynamic classes

Source: Internet
Author: User
Tags emit

We use a computingA + BTo demonstrate how to add attributes, constructor, and method to a dynamic class, and to use the attributes defined in the class in the method, first, we will give the dynamic class to be implementedC #CodeAnd thenC #The Code provides the corresponding implementation. The Code is as follows:

Add
Public   Class Add

{

Private Int_= 0;

Public IntA

{

Get{Return_ ;}

Set{_=Value ;}

}

 

Private Int_ B= 0;

Public IntB

{

Get{Return_ B ;}

Set{_ B=Value ;}

}

 

PublicAdd (IntA,IntB)

{

_=A;

_ B=B;

}

 

Public IntCalc ()

{

Return_+_ B;

}

The previous steps are the same as the previous Fibonacci implementation. Here we start with the attribute definition. First, we use Typebuilder Object Definefield Method To define private fields _ And _ B And set the default values for them. 0 The Code is as follows:

Define private fields _ A and _ B
// Define private fields _ A and _ B

Fieldbuilder fieldabuilder = Typebuilder. definefield ( " _ " , Typeof (Int32), fieldattributes. Private );

Fieldbuilder fieldbbuilder=Typebuilder. definefield ("_ B",Typeof(Int32), fieldattributes. Private );

Fieldabuilder. setconstant (0);

Fieldbbuilder. setconstant (0);

Then we use Typebuilder Object Defineproperty Methods define attributes respectively A , B ; Then pass Propertybuilder Of Setgetmethod And Setsetmethod Method to set their Get And Set Method, as Get And Set Method Il Code Generation is equivalent Method The definitions are the same. Only attributes are listed here. A Definition, Properties B And A The Code is as follows:

Define public attributes A and B
// Define public attributes A and B

Propertybuilder propertyabuilder = Typebuilder. defineproperty ( " A " , Propertyattributes. None, Typeof (Int32 ), Null );

Propertybuilder propertybbuilder=Typebuilder. defineproperty ("B", Propertyattributes. None,Typeof(Int32 ),Null);

 

# RegionDefine the get and set methods of attribute.

 

//Define the get method of attribute

Methodbuilder getpropertyabuilder=Typebuilder. definemethod ("Get",

Methodattributes. Public|Methodattributes. specialname|Methodattributes. hidebysig,

Typeof(Int32 ),

Type. emptytypes );

 

//The IL code that generates the get method of attribute a, that is, the private field _ A is returned.

Ilgenerator getail=Getpropertyabuilder. getilgenerator ();

 

Getail. emit (Opcodes. ldarg_0 );

Getail. emit (Opcodes. lddes, fieldabuilder );

Getail. emit (Opcodes. Ret );

 

//Set Method for defining attribute

Methodbuilder setpropertyabuilder=Typebuilder. definemethod ("Set",

Methodattributes. Public|Methodattributes. specialname|Methodattributes. hidebysig,

Null,

NewType [] {Typeof(Int32 )});

 

//Generate the Il code of the Set Method of attribute a, that is, set the value of private field _ A to the value of input parameter 1.

Ilgenerator setail=Setpropertyabuilder. getilgenerator ();

 

Setail. emit (Opcodes. ldarg_0 );

Setail. emit (Opcodes. ldarg_1 );

Setail. emit (Opcodes. stdes, fieldabuilder );

Setail. emit (Opcodes. Ret );

 

//Set the get and set methods of attribute.

Propertyabuilder. setgetmethod (getpropertyabuilder );

Propertyabuilder. setsetmethod (setpropertyabuilder );

 

# Endregion

last, let's define the constructor and calc MSO-Hansi-font-family: "Times New Roman" "> method, typebuilder defineconstructor "Times New Roman" "> method, obtain a constructorbuilder "Times New Roman"> the object is transferred to the same step as the normal method definition, the Code is as follows:

Define constructors and Methods
# Region Step 5 define Constructors

Constructorbuilder=Typebuilder. defineconstructor (methodattributes. Public, callingconventions. hasthis,NewType [] {Typeof(Int32 ),Typeof(Int32 )});

Ilgenerator ctoril=Constructorbuilder. getilgenerator ();

//Load parameter 1 to private field _
Ctoril. emit (Opcodes. ldarg_0 );
Ctoril. emit (Opcodes. ldarg_1 );
Ctoril. emit (Opcodes. stdes, fieldabuilder );
//Add parameter 2 to private field _ B
Ctoril. emit (Opcodes. ldarg_0 );
Ctoril. emit (Opcodes. ldarg_2 );
Ctoril. emit (Opcodes. stdes, fieldbbuilder );
Ctoril. emit (Opcodes. Ret );

# Endregion

# RegionStep 6 definition method

Methodbuilder calcmethodbuilder=Typebuilder. definemethod ("Calc", Methodattributes. Public,Typeof(Int32), type. emptytypes );

Ilgenerator calcer=Calcmethodbuilder. getilgenerator ();

//Load private field _
Calcer. emit (Opcodes. ldarg_0 );
Calcer. emit (Opcodes. ldcil, fieldabuilder );
//Load private field _ B
Calcer. emit (Opcodes. ldarg_0 );
Calcer. emit (Opcodes. ldcil, fieldbbuilder );
//Add and return results
Calcer. emit (Opcodes. Add );
Calcer. emit (Opcodes. Ret );

# Endregion


At this point, we finally completed the creation of dynamic classes, and finally provided the complete source code download. A + B "Times New Roman"; MSO-font-kerning: 6.5pt; MSO-ANSI-language: En-US; MSO-Fareast-language:
ZH-CN; MSO-bidi-language: AR-SA ">, hope this series of articles can be helpful to everyone.

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.