CHAPTER 18 UNDERSTANDING CIL AND THE ROLE OF DYNAMIC ASSEMBLIES
Interface
Specifies that the type is an interface
NestedAssembly
Specifies that the class is nested with assembly visibility and is thus
accessible only by methods within its assembly
NestedFamAndAssem
Specifies that the class is nested with assembly and family visibility, and is
thus accessible only by methods lying in the intersection of its family and
assembly
NestedFamily
Specifies that the class is nested with family visibility and is thus accessible
only by methods within its own type and any subtypes
NestedFamORAssem
Specifies that the class is nested with family or assembly visibility, and is
thus accessible only by methods lying in the union of its family and assembly
NestedPrivate
Specifies that the class is nested with private visibility
NestedPublic
Specifies that the class is nested with public visibility
NotPublic
Specifies that the class is not public
Public
Specifies that the class is public
Sealed
Specifies that the class is concrete and cannot be extended
Serializable
Specifies that the class can be serialized
Emitting the HelloClass Type and the String Member Variable
Now that you have a better understanding of the role of the ModuleBuilder.CreateType() method, let’s
examine how you can emit the public HelloWorld class type and the private string variable.
// Define a public class named "MyAssembly.HelloWorld".
TypeBuilder helloWorldClass = module.DefineType("MyAssembly.HelloWorld",
TypeAttributes.Public);
// Define a private String member variable named "theMessage".
FieldBuilder msgField =
helloWorldClass.DefineField("theMessage",
typeof(string),
FieldAttributes.Private);
Notice how the TypeBuilder.DefineField() method provides access to a FieldBuilder type. The
TypeBuilder class also defines other methods that provide access to other “builder” types. For example,
DefineConstructor() returns a ConstructorBuilder, DefineProperty() returns a PropertyBuilder, and so forth.
690