home comics writing pictures archive about

2024-05-25 - In IL: Field Declarations

In IL

Last time we looked at the options for declaring fields in IL, today we are going to look at some examples.

This C# program has a variety of fields defined.

Program.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace CsFields
{
internal class Program
{
private int privateField;
public int publicField;
internal int internalField;
protected int protectedField;
private protected int privateProtectedField;
protected internal int protectedInternalFIeld;
const int constantField = 5;
readonly int readOnlyField;
static int staticField;
FileMode enumField;
FileInfo classField;
Complex structField;
string stringField;
double doubleField;
int intField;
FileMode[] enumArrayField;
FileInfo[] classArrayField;
Complex[] structArrayField;
string[] stringArrayField;
static void Main(string[] args)
{
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

Let's start by looking at the accessibility attributes.

.field private int32 privateField
.field public int32 publicField
.field assembly int32 internalField
.field family int32 protectedField
.field famandassem int32 privateProtectedField
.field famorassem int32 protectedInternalFIeld
63
64
65
66
67
68

As expected we have the .field directive, the accessibility attribute, the type and the name of the field. The accessibility attributes line up the same as they did for the nested classes that we looked at previously.

Next we have some examples of the contract attributes.

.field private static literal int32 constantField = int32(0x00000005)
.field private initonly int32 readOnlyField
.field private static int32 staticField
69
70
71

Again this is as we expect. All the fields are private because we omitted the accessibility marker. The const field became static literal to indicate that it is stored with the class and it can only have the value specified. Note that the literal assigned to the field is wrapped in a type specifier. readonly became initonly with both meaning that the field can only be set in a constructor. The static field is also marked as static in IL.

Now let's look at some different type identifiers.

.field private valuetype [mscorlib]System.IO.FileMode enumField
.field private class [mscorlib]System.IO.FileInfo classField
.field private valuetype [System.Numerics]System.Numerics.Complex structField
.field private string stringField
.field private float64 doubleField
.field private int32 intField
72
73
74
75
76
77

First we have the FileMode field, in IL this is marked valuetype to indicate it's a value type (it's an enum) and then we get the fully qualified name of the type [mscorlib]System.IO.FileMode. The [mscorlib] part indicates the assembly that contains the type. The FileInfo field is marked class to indicate that it's a reference type. The Complex field is also marked as valuetype because it's a struct and we can see it's from a different assembly ([System.Numerics]) than the others. Finally we have the built in types which just replace the C# name for the type with the IL name for the time.

To finish off, let's look at some array fields.

.field private valuetype [mscorlib]System.IO.FileMode[] enumArrayField
.field private class [mscorlib]System.IO.FileInfo[] classArrayField
.field private valuetype [System.Numerics]System.Numerics.Complex[] structArrayField
.field private string[] stringArrayField
78
79
80
81

Arrays are indicated by the [] at the end of the type name the same way they are in C#.

Well this was all very straight forward. Next time we will look at the instructions used to work with fields

Comments: