【. NET underlying anatomy "stfld directives-assigning values to fields of an object

Source: Internet
Author: User
Tags mscorlib

. NET underlying anatomy directory Chapter

Introduction:

In this article we explain the frequently seen directive stfld in the. NET IL Intermediate language.

This directive is often used to assign values to fields of an object.

I. Purpose of instruction:

MSDN explains the following:

Replaces the value stored in the field of an object reference or pointer with a new value.

Replace the value of the Object field with a new value.

II. Namespaces and Assemblies

The namespace is inside the System.Reflection.Emit .

Assembly is mscorlib (in mscorlib.dll )

III. instruction Execution mechanism

How it works is the stack transition behavior:

In chronological order:

1. pressing an object reference or pointer onto the stack

2. pressing values into the stack

3. the value and the object's reference / pointer are popped off the stack, the field of the object is updated to the replaced value

Iv. Example Code Analysis:

C # Program:

namespaceconsoleapplication1{classProgram {Static voidMain (string[] args) {Test test1=NewTest ();//new a Test objectTEST1.I = A;//assigns the field I of the test object to a value of        }        /// <summary>        ///Test Class/// </summary>         Public classTest { Public inti = -; }    }}

IL Program Main method

. Method private hidebysig static void Main (string[] args) CIL managed{. entrypoint//Code size -(0x10). Maxstack2. Locals init ([0] class consoleapplication1.program/Testtest1)il_0000:  NOP  il_0001:newobj instance void consoleapplication1.program/Test::. ctor ()il_0006:Stloc.0  il_0007:Ldloc.0  il_0008:Ldc.i4.s A  il_000a:stfld int32 consoleapplication1.program/Test:: Iil_000f:  ret}//End of methodProgram :: Main

Let's analyze the IL code of the main method on a row-by- line basis

. EntryPoint // Define the entry point of the function

Code size (0x10)// size

. maxstack 2// stack size is 2

. Locals init ([0] class consoleapplication1.program/test test1) // define a variable as test1, stored in the first variable in the < local variable list >

Il_0000:nop // empty operation

il_0001:newobj instance void Consoleapplication1.program/test::.ctor () //new A Test object, a reference to the object, a reference to the stack,

Objects are stored above the heap

il_0006:stloc.0 // The reference stack is stored in the first variable in the < local variables list >

il_0007:ldloc.0 // Press the value of the first variable in the < local variable list > into the Stack

Il_0008:ldc.i4.s // int is pressed into the stack

il_000a:stfld Int32 consoleapplication1.program/test::i//assigns the value of the stack's top stack to the second value of the stack, which is test.i=12

Il_000f:ret // function return

Five, Memory analysis

Memory diagram prior to execution of instruction STFLD

The stack holds the address of the Test1, the < local variable list > The first variable holds the address of Test1 , and the heap is stored in test1 point to an object where test1.i=100

Memory diagram after instruction stfld execution

The test1 address pops up in the stack, the < local variable list > The first variable holds the test1 address unchanged, and the heap holds the test1.i=12

Vi. Summary

This article is mainly about how the field of an object is assigned in memory, and the execution of stfld from each line of IL instruction . You can see the process of assignment more clearly from the assignment of the field of the underlying analysis object.

Next I will be from the. NET bottom of the analysis parameters of the delivery, interested can pay attention to me Oh!

". NET underlying anatomy "stfld directives-assigning values to fields of objects

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.