home comics writing pictures archive about

2021-06-06 - In IL: Class Definitions

In IL

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: