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

0 Comments:

Post a Comment

<< Home