text stringlengths 0 13.4k |
|---|
{ |
m_number = initNumber; |
} |
public int Number |
{ |
get { return m_number; } |
set { m_number = value; } |
} |
public int MyMethod(int multiplier) |
{ |
return m_number * multiplier; |
} |
} |
*/ |
AssemblyName aName = new AssemblyName("DynamicAssemblyExample"); |
AssemblyBuilder ab = |
AppDomain.CurrentDomain.DefineDynamicAssembly( |
aName, |
AssemblyBuilderAccess.RunAndSave); |
// For a single-module assembly, the module name is usually |
// the assembly name plus an extension. |
ModuleBuilder mb = |
ab.DefineDynamicModule(aName.Name, aName.Name + ".dll"); |
TypeBuilder tb = mb.DefineType( |
"MyDynamicType", |
TypeAttributes.Public); |
GoalKicker.com – .NET Framework Notes for Professionals |
83 |
// Add a private field of type int (Int32). |
FieldBuilder fbNumber = tb.DefineField( |
"m_number", |
typeof(int), |
FieldAttributes.Private); |
// Next, we make a simple sealed method. |
MethodBuilder mbMyMethod = tb.DefineMethod( |
"MyMethod", |
MethodAttributes.Public, |
typeof(int), |
new[] { typeof(int) }); |
ILGenerator il = mbMyMethod.GetILGenerator(); |
il.Emit(OpCodes.Ldarg_0); // Load this - always the first argument of any instance method |
il.Emit(OpCodes.Ldfld, fbNumber); |
il.Emit(OpCodes.Ldarg_1); // Load the integer argument |
il.Emit(OpCodes.Mul); // Multiply the two numbers with no overflow checking |
il.Emit(OpCodes.Ret); // Return |
// Next, we build the property. This involves building the property itself, as well as the |
// getter and setter methods. |
PropertyBuilder pbNumber = tb.DefineProperty( |
"Number", // Name |
PropertyAttributes.None, |
typeof(int), // Type of the property |
new Type[0]); // Types of indices, if any |
MethodBuilder mbSetNumber = tb.DefineMethod( |
"set_Number", // Name - setters are set_Property by convention |
// Setter is a special method and we don't want it to appear to callers from C# |
MethodAttributes.PrivateScope | MethodAttributes.HideBySig | MethodAttributes.Public | |
MethodAttributes.SpecialName, |
typeof(void), // Setters don't return a value |
new[] { typeof(int) }); // We have a single argument of type System.Int32 |
// To generate the body of the method, we'll need an IL generator |
il = mbSetNumber.GetILGenerator(); |
il.Emit(OpCodes.Ldarg_0); // Load this |
il.Emit(OpCodes.Ldarg_1); // Load the new value |
il.Emit(OpCodes.Stfld, fbNumber); // Save the new value to this.m_number |
il.Emit(OpCodes.Ret); // Return |
// Finally, link the method to the setter of our property |
pbNumber.SetSetMethod(mbSetNumber); |
MethodBuilder mbGetNumber = tb.DefineMethod( |
"get_Number", |
MethodAttributes.PrivateScope | MethodAttributes.HideBySig | MethodAttributes.Public | |
MethodAttributes.SpecialName, |
typeof(int), |
new Type[0]); |
il = mbGetNumber.GetILGenerator(); |
il.Emit(OpCodes.Ldarg_0); // Load this |
il.Emit(OpCodes.Ldfld, fbNumber); // Load the value of this.m_number |
il.Emit(OpCodes.Ret); // Return the value |
pbNumber.SetGetMethod(mbGetNumber); |
// Finally, we add the two constructors. |
// Constructor needs to call the constructor of the parent class, or another constructor in |
GoalKicker.com – .NET Framework Notes for Professionals |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.