[. Net underlying analysis] stfld command-assigns a value to the object field,. netstfld

Source: Internet
Author: User
Tags mscorlib

[. Net underlying analysis] stfld command-assigns a value to the object field,. netstfld

. Net underlying analysis directory chapter

Introduction:

In this article, we will explain the common stfld commands in the. net IL intermediate language.

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

I. Command purpose:

The MSDN explanation is as follows:

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. namespace and assembly

The namespace is in System. Reflection. Emit.

The Assembly is mscorlib (in mscorlib. dll)

Iii. Command Execution Mechanism

The stack conversion is as follows:

In order:

1. Push an object reference or pointer to the stack

2. Press the value into the stack.

3. This value and the reference/pointer of the object are popped up from the stack. The object field is updated to the replaced value.

4. instance code analysis:

C # program:

Namespace ConsoleApplication1 {class Program {static void Main (string [] args) {Test test1 = new Test (); // a new Test object test1. I = 12; // assign the field I of the Test object to 12} // <summary> // Test class // </summary> public class Test {public int I = 100 ;} }}

 

Main method of IL Program

.method private hidebysig static void  Main(string[] args) cil managed{  .entrypoint  // Code size       16 (0x10)  .maxstack  2  .locals init ([0] class ConsoleApplication1.Program/Test test1)  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   12  IL_000a:  stfld      int32 ConsoleApplication1.Program/Test::i  IL_000f:  ret} // end of method Program::Main

Let's analyze the IL code of the main method line by line.

. Entrypoint// Define the function entry point

// Code size 16 (0x10) // The Code size is 16

. Maxstack2// Stack size is 2

. Locals init ([0] class ConsoleApplication1.Program/Test test1)// Define a variable as test1 and store it in the first variable in <local variable list>

IL_0000: nop// Null operation

 IL_0001: newobj instance void ConsoleApplication1.Program/Test:. ctor ()// A new Test object, a reference pointing to this object, the reference is stored on the stack,

Objects are stored on the heap.

 IL_0006: stloc.0// Store the reference stack to the first variable in the <local variable list>

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

IL_0008: ldc. i4.s 12// Press int 12 into the stack

 IL_000a: stfldInt32 ConsoleApplication1.Program/Test: I // assign the top value of the stack to the second value of the stack, that is, test. I = 12.

IL_000f: ret// Function return

5. Memory Analysis

Memory diagram before the execution of the command stfld

 

 

Store the address of 12 and test1 in the stack. <local variable list> the first variable stores the address of test1, And the heap stores an object pointed to by test1.Test1. I = 100

 

Memory diagram after stfld Command Execution

 

 

 

 

The address of 12 and test1 in the stack is displayed. <local variable list> the address of test1 is retained in the first variable, and the address of test1 is retained in the heap.Test1. I = 12

 

Vi. Summary

This article describes how to assign values to object fields in the memory and analyze the execution process of stfld from each line of IL commands. From the field assignment of the underlying analysis object, you can see the assignment process more clearly.

 

In the next article, I will analyze the parameter transfer from the bottom layer of. net. If you are interested, follow me!

 

Related Article

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.