2021-06-06 - In IL: Class Definitions
In IL
- Part 1 - Introduction
- Part 2 - Variables and Types
- Part 3 - Variables in Visual Basic .NET
- Part 4 - Instructions and the Stack
- Part 5 - Volume of a Cylinder (Operations)
- Part 6 - Branching Instructions
- Part 7 - Largest of Two Numbers (if-else)
- Part 8 - Largest of Three Numbers (If-ElseIf-Else)
- Part 9 - Switch instruction
- Part 10 - Grade Analyser (switch)
- Part 11 - Prize Calculator (switch-2)
- Part 12 - VB Grade Analyser (Select)
- Part 13 - Loop Instructions
- Part 14 - Print the Alphabet (while)
- Part 15 - Print the Alphabet (do while, for)
- Part 16 - Print the Alphabet (Do Until)
- Part 17 - Print the Alphabet (break, continue)
- Part 18 - Array Instructions
- Part 19 - Summing Arrays
- Part 20 - Other Instructions
- Part 21 - Assemblies
- Part 22 - Class Definitions
- Part 23 - C# Classes and Structs
- Part 24 - VB Classes, Modules and Structures
- Part 25 - Field Definitions
- Part 26 - Field Declarations
IL is a object oriented language like C# and Visual Basic .Net. This means that classes are a built in concept. Classes are defined with a class header describing the class followed by a class body enclosed in curly braces.
.class <attributes..> <Id> extends <baseClas> implements <interfaces…>{ }
A class is declared using the .class directive. This is followed by a set of attributes which describe how the class behaves. The Id is the name of the class. The extends keyword is used to specific the base class. If no base class is provided system.Object is assumed. The implements keyword is used to specify which interfaces the class supports.
There are a variety of attributes that can be set.
Visibility
The visibility attributes are used for non-nested classes to indicate if they are exported, available, outside the assembly or not.
Attribute | Description |
---|---|
private | Class is not exported outside assembly |
public | Class is exported outside assembly |
Accessibility
The accessibility attributes are used for nested classes to indicate if they are available to other classes.
Attribute | Description |
---|---|
nested private | Visible only within the containing class |
nested famandassem | Visible to containing class and derived classes within the same assembly |
nested assembly | Visible to the containing assembly |
nested family | Visibility to containing class and derived classes |
nested famorassem | Visibility to containing class and derived classes or to the containing assembly |
nested public | Visible everywhere |
Layout
The layout attributes control how the fields of the class will be laid out in memory
Attribute | Description |
---|---|
auto | Layout of fields in the class is determined by the runtime |
sequential | Layout of fields will be sequential based on order of the fields in the class |
explicit | Layout of fields in the class will be set explicitly |
Type
The type attribute is used to specify what type of class is being defined
Attribute | Description |
---|---|
interface | Class is an interface |
Inheritance
Inheritance attributes control how a class can be used
Attribute | Description |
---|---|
abstract | Class cannot be instantiated |
sealed | Class cannot be derived from |
Interpolation
Interpolation attributes control how the class is handled when interacting with unmanaged code. Specifically it controls how strings contained in the class will be marshalled, packaged, before being sent to the unmanaged code.
Attribute | Description |
---|---|
autochar | string marshalling is determined by platform |
ansi | Marshalled using ansi strings |
unicode | Marshalled using UTF-16 strings |
Special
Special attributes are used by the runtime or tool to control how the class will be treated
Attribute | Description |
---|---|
beforefieldinit | Don’t initialize fields before a static call |
serializable | fields can be serialized (reserved) |
rtspecialname | Name has special meaning to the runtime |
specialname | Name has special meaning to tools |
Comments: