DotNet Programming World

Tuesday, July 10, 2007

Other Builders

public static void CreateStudentConstructor
(TypeBuilder yourTypeBuilder)
{
ConstructorBuilder constructor =
yourTypeBuilder.DefineConstructor(
MethodAttributes.Public |
MethodAttributes.SpecialName |
MethodAttributes.RTSpecialName,
CallingConventions.Standard,
Type.EmptyTypes);

}//End CreateStudentConstructor

public static void CreateFullNameField
(TypeBuilder stuTypeBuilder)
{
FieldBuilder stuFullNameFieldBuilder =
stuTypeBuilder.DefineField("FullName",
typeof(string), FieldAttributes.Private);

}//End CreateFullNameField

public static void CreateFullNameProperty
(TypeBuilder stuTypeBuilder)
{
PropertyBuilder stuFullNamePropBuilder =
stuTypeBuilder.DefineProperty("FullName",
PropertyAttributes.None, typeof(string), null);

MethodAttributes getSetAttr =
MethodAttributes.Public | MethodAttributes.SpecialName |
MethodAttributes.HideBySig;

MethodBuilder stuFullNameGetPropMethodBuilder =
stuTypeBuilder.DefineMethod("get_FullName",
getSetAttr, typeof(string), Type.EmptyTypes);

ILGenerator custNameGetIL =
stuFullNameGetPropMethodBuilder.GetILGenerator();

custNameGetIL.Emit(OpCodes.Ldarg_0);
custNameGetIL.Emit(OpCodes.Ldfld,
stuFullNameGetPropMethodBuilder);
custNameGetIL.Emit(OpCodes.Ret);

MethodBuilder stuFullNameSetPropMethodBuilder =
stuTypeBuilder.DefineMethod("set_FullName",
getSetAttr, null, new Type[] { typeof(string) });

ILGenerator custNameSetIL =
stuFullNameSetPropMethodBuilder.GetILGenerator();

custNameSetIL.Emit(OpCodes.Ldarg_0);
custNameSetIL.Emit(OpCodes.Ldarg_1);
custNameSetIL.Emit(OpCodes.Stfld,
stuFullNameGetPropMethodBuilder);
custNameSetIL.Emit(OpCodes.Ret);

stuFullNamePropBuilder.SetGetMethod
(stuFullNameGetPropMethodBuilder);
stuFullNamePropBuilder.SetSetMethod
(stuFullNameSetPropMethodBuilder);
}//End CreateFullNameProperty

public static void CreateMessageParameter
(MethodBuilder yourMethodBuilder)
{

ParameterBuilder yourMessage =
yourMethodBuilder.DefineParameter(1,
ParameterAttributes.In, "yourMessage");

}//End CreateMessageParameter

public static void CreateGenericParameter
(MethodBuilder yourMethodBuilder)
{

string[] typeParamNames =
{ "TOne", "TTwo" };
GenericTypeParameterBuilder[] typeParams =
yourMethodBuilder.DefineGenericParameters
(typeParamNames);

}//End CreateGenericParameter

public static void CreateLocalVariables
(MethodBuilder sayHelloMethod)
{
ILGenerator myMethodIL =
sayHelloMethod.GetILGenerator();

LocalBuilder localStringVariable =
myMethodIL.DeclareLocal(typeof(string));
localStringVariable.SetLocalSymInfo("myString");

LocalBuilder localIntVariable =
myMethodIL.DeclareLocal(typeof(int));
localIntVariable.SetLocalSymInfo("myInt", 1, 2);

myMethodIL.Emit(OpCodes.Ldstr, "Local value");
myMethodIL.Emit(OpCodes.Stloc_0);
myMethodIL.Emit(OpCodes.Ldloc_0);
myMethodIL.Emit(OpCodes.Stloc_1);
myMethodIL.Emit(OpCodes.Ldloc_1);
myMethodIL.Emit(OpCodes.Ret);


}//End CreateLocalVariables

Type Builder

public static TypeBuilder CreateStudentTypeBuilder
(ModuleBuilder stuModuleBuilder)
{
TypeBuilder stuTypeBuilder =
stuModuleBuilder.DefineType("Student",
TypeAttributes.Public);
return stuTypeBuilder;
}//End CreateStudentTypeBuilder

Enum Builder

public static void CreateEnumForStudentType
(ModuleBuilder yourModuleBuilder)
{
EnumBuilder myEnumBuilder;

myEnumBuilder = yourModuleBuilder.DefineEnum
("DynamicDemo.StudentType", TypeAttributes.Public,
typeof(Int32));

FieldBuilder underGradField =
myEnumBuilder.DefineLiteral("Undergraduate", 1);
FieldBuilder gradField =
myEnumBuilder.DefineLiteral("Graduate", 2);

myEnumBuilder.CreateType();

}//End CreateEnumForStudent

Module Builder

public static ModuleBuilder CreateModule
(AssemblyBuilder stuAssemblyBuilder,
AssemblyName stuAssemblyName)
{
ModuleBuilder myModBuilder =
stuAssemblyBuilder.DefineDynamicModule
(stuAssemblyName.Name, stuAssemblyName.Name + ".dll");
return myModBuilder;
}//End CreateModule

Create Assemblies Dynamically by Using Builder Classes

public static void CreateStudentDynamicAssembly()
{
AppDomain myDomain = Thread.GetDomain();

AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "CreatingDynamicAssembliesDemo";

AssemblyBuilder myAssemblyBuilder = myDomain.DefineDynamicAssembly(myAssemblyName,
AssemblyBuilderAccess.RunAndSave);


ModuleBuilder stuModule = CreateModule(myAssemblyBuilder, myAssemblyName);
TypeBuilder stuTypeBuilder = CreateStudentTypeBuilder(stuModule);

CreateSayHelloMethod(stuTypeBuilder);
CreateFullNameField(stuTypeBuilder);
CreateFullNameProperty(stuTypeBuilder);


myAssemblyBuilder.Save(myAssemblyName.Name + ".dll");

}//End CreateDynamicAssembly

Access Type metadata by using Assembly

using System;
using System.Reflection;

static public void ListOtherTypesInAssembly(Type yourType)
{
Assembly sampleAssembly;
Type[] publicTypesInAssembly;
AssemblyName[] referencedAssemblies;

try
{
sampleAssembly = Assembly.GetAssembly(yourType);

publicTypesInAssembly = sampleAssembly.GetExportedTypes();
referencedAssemblies = sampleAssembly.GetReferencedAssemblies();
Console.WriteLine("Type name: '{0}' is contained in assembly ", yourType.Name);
Console.WriteLine(sampleAssembly.FullName);
Console.WriteLine();
Console.WriteLine("This assembly references {0} other assemblies", referencedAssemblies.GetLength(0));
Console.WriteLine("There are {0} public types in this assembly.", publicTypesInAssembly.GetLength(0));
Console.WriteLine();
Console.ReadLine();

Console.WriteLine("Referenced Assemblies:");
Console.WriteLine();

foreach (AssemblyName name in referencedAssemblies)
{
Console.WriteLine(name.FullName);
Console.WriteLine();
}
Console.ReadLine();

Console.WriteLine("Public types in this assembly:");
foreach (Type t in publicTypesInAssembly)
{
Console.WriteLine(t.Name);
}

}//End try
catch (Exception xcp)
{
Console.WriteLine(xcp.ToString());
}
finally
{
Console.ReadLine();
}
}