AssemblyBuilder Class
Defines and represents a dynamic assembly.
Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)
The AssemblyBuilder type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | CodeBase | Gets the location of the assembly as specified originally, for example, in an AssemblyName object. (Inherited from Assembly.) |
![]() | EntryPoint | Gets the entry point of this assembly. (Overrides Assembly.EntryPoint.) |
![]() | FullName | Gets the display name of the current dynamic assembly. (Overrides Assembly.FullName.) |
![]() | ImageRuntimeVersion | Gets the version of the common language runtime that will be saved in the file containing the manifest. (Overrides Assembly.ImageRuntimeVersion.) |
![]() | IsDynamic | Gets a value that indicates that the current assembly is a dynamic assembly. (Overrides Assembly.IsDynamic.) |
![]() | Location | Gets the path or UNC location of the loaded file that contains the manifest. (Inherited from Assembly.) |
![]() | ManifestModule | Gets the module in the current AssemblyBuilder that contains the assembly manifest. (Overrides Assembly.ManifestModule.) |
| Name | Description | |
|---|---|---|
![]() | CreateInstance | Locates the specified type from this assembly and creates an instance of it using the system activator, using case-sensitive search. (Inherited from Assembly.) |
![]() | DefineDynamicModule(String) | Defines a named transient dynamic module in this assembly. |
![]() | DefineDynamicModule(String, Boolean) | Defines a named transient dynamic module in this assembly and specifies whether symbol information should be emitted. |
![]() | Equals | Returns a value that indicates whether this instance is equal to the specified object. (Overrides Object.Equals(Object).) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetCustomAttributes(Boolean) | Returns all the custom attributes that have been applied to the current AssemblyBuilder. (Overrides Assembly.GetCustomAttributes(Boolean).) |
![]() | GetCustomAttributes(Type, Boolean) | Returns all the custom attributes that have been applied to the current AssemblyBuilder, and that derive from a specified attribute type. (Overrides Assembly.GetCustomAttributes(Type, Boolean).) |
![]() | GetDynamicModule | Returns the dynamic module that has the specified name. |
![]() | GetExportedTypes | Gets the exported types that are defined in this assembly. (Overrides Assembly.GetExportedTypes().) |
![]() | GetFile | Security Critical. Gets a FileStream for the specified file in the file table of the manifest of this assembly. (Inherited from Assembly.) |
![]() | GetFiles | Security Critical. Gets the files in the file table of an assembly manifest, specifying whether to include resource modules. (Inherited from Assembly.) |
![]() | GetHashCode | Returns the hash code for this instance. (Overrides Object.GetHashCode().) |
![]() | GetManifestResourceNames | Loads the names of the manifest resources in this assembly. (Overrides Assembly.GetManifestResourceNames().) |
![]() | GetManifestResourceStream(String) | Loads the specified manifest resource from this assembly. (Overrides Assembly.GetManifestResourceStream(String).) |
![]() | GetManifestResourceStream(Type, String) | Loads the specified manifest resource, scoped by the namespace of the specified type, from this assembly. (Overrides Assembly.GetManifestResourceStream(Type, String).) |
![]() | GetModules | Gets all the modules that are part of this assembly. (Inherited from Assembly.) |
![]() | GetName | Security Critical. Gets the AssemblyName that was specified when the current dynamic assembly was created, and sets the code base as specified. (Overrides Assembly.GetName(Boolean).) |
![]() | GetSatelliteAssembly(CultureInfo) | Gets the satellite assembly for the specified culture. (Overrides Assembly.GetSatelliteAssembly(CultureInfo).) |
![]() | GetSatelliteAssembly(CultureInfo, Version) | Gets the specified version of the satellite assembly for the specified culture. (Overrides Assembly.GetSatelliteAssembly(CultureInfo, Version).) |
![]() | GetType() | Gets the Type of the current instance. (Inherited from Object.) |
![]() | GetType(String) | Gets the Type object with the specified name in the assembly instance. (Inherited from Assembly.) |
![]() | GetType(String, Boolean) | Gets the Type object with the specified name in the assembly instance and optionally throws an exception if the type is not found. (Inherited from Assembly.) |
![]() | GetTypes | Gets the types defined in this assembly. (Inherited from Assembly.) |
![]() | IsDefined | Returns a value that indicates whether one or more instances of the specified attribute type is applied to this member. (Overrides Assembly.IsDefined(Type, Boolean).) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | SetCustomAttribute | Sets a custom attribute on this assembly by using a custom attribute builder. |
![]() | ToString | Returns the full name of the assembly, also known as the display name. (Inherited from Assembly.) |
A dynamic assembly is an assembly that is created using the reflection emit APIs. In Silverlight, all dynamic assemblies are transient, and therefore cannot be saved. A dynamic assembly contains only one dynamic module.
To get an AssemblyBuilder object, use the AppDomain.DefineDynamicAssembly method.
The following example shows how to define a dynamic assembly with one module. The module in the example assembly contains one type, MyDynamicType, which has a private field, a property that gets and sets the private field, constructors that initialize the private field, and a method that multiplies a user-supplied number by the private field value and returns the result.
Note: |
|---|
To run this example, see Building Examples That Use a Demo Method and a TextBlock Control. |
using System; using System.Reflection; using System.Reflection.Emit; class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { // In this version of the runtime, an assembly consists of one // module which contains zero or more types. This example // creates an assembly containing one public type named // "MyDynamicType". The type has a private field, a property // that gets and sets the private field, constructors that // initialize the private field, and a method that multiplies // a user-supplied number by the private field value and returns // the result. In C# the type might look like this: /* public class MyDynamicType { private int m_number; public MyDynamicType() : this(42) {} public MyDynamicType(int initNumber) { 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.Run); // Create the module. ModuleBuilder mb = ab.DefineDynamicModule(aName.Name); TypeBuilder tb = mb.DefineType( "MyDynamicType", TypeAttributes.Public); // Add a private field of type int (Int32). FieldBuilder fbNumber = tb.DefineField( "m_number", typeof(int), FieldAttributes.Private); // Define a constructor that takes an integer argument and // stores it in the private field. Type[] parameterTypes = { typeof(int) }; ConstructorBuilder ctor1 = tb.DefineConstructor( MethodAttributes.Public, CallingConventions.Standard, parameterTypes); ILGenerator ctor1IL = ctor1.GetILGenerator(); // For a constructor, argument zero is a reference to the new // instance. Push it on the stack before calling the base // class constructor. Specify the default constructor of the // base class (System.Object) by passing an empty array of // types (Type.EmptyTypes) to GetConstructor. ctor1IL.Emit(OpCodes.Ldarg_0); ctor1IL.Emit(OpCodes.Call, typeof(object).GetConstructor(Type.EmptyTypes)); // Push the instance on the stack before pushing the argument // that is to be assigned to the private field m_number. ctor1IL.Emit(OpCodes.Ldarg_0); ctor1IL.Emit(OpCodes.Ldarg_1); ctor1IL.Emit(OpCodes.Stfld, fbNumber); ctor1IL.Emit(OpCodes.Ret); // Define a default constructor that supplies a default value // for the private field. For parameter types, pass the empty // array of types or pass null. ConstructorBuilder ctor0 = tb.DefineConstructor( MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes); ILGenerator ctor0IL = ctor0.GetILGenerator(); // For a constructor, argument zero is a reference to the new // instance. Push it on the stack before pushing the default // value on the stack, then call constructor ctor1. ctor0IL.Emit(OpCodes.Ldarg_0); ctor0IL.Emit(OpCodes.Ldc_I4_S, 42); ctor0IL.Emit(OpCodes.Call, ctor1); ctor0IL.Emit(OpCodes.Ret); // Define a property named Number that gets and sets the private // field. // // The last argument of DefineProperty is null, because the // property has no parameters. (If you don't specify null, you must // specify an array of Type objects. For a parameterless property, // use the built-in array with no elements: Type.EmptyTypes) PropertyBuilder pbNumber = tb.DefineProperty( "Number", PropertyAttributes.HasDefault, typeof(int), null); // The property "set" and property "get" methods require a special // set of attributes. MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; // Define the "get" accessor method for Number. The method returns // an integer and has no arguments. (Note that null could be // used instead of Types.EmptyTypes) MethodBuilder mbNumberGetAccessor = tb.DefineMethod( "get_Number", getSetAttr, typeof(int), Type.EmptyTypes); ILGenerator numberGetIL = mbNumberGetAccessor.GetILGenerator(); // For an instance property, argument zero is the instance. Load the // instance, then load the private field and return, leaving the // field value on the stack. numberGetIL.Emit(OpCodes.Ldarg_0); numberGetIL.Emit(OpCodes.Ldfld, fbNumber); numberGetIL.Emit(OpCodes.Ret); // Define the "set" accessor method for Number, which has no return // type and takes one argument of type int (Int32). MethodBuilder mbNumberSetAccessor = tb.DefineMethod( "set_Number", getSetAttr, null, new Type[] { typeof(int) }); ILGenerator numberSetIL = mbNumberSetAccessor.GetILGenerator(); // Load the instance and then the numeric argument, then store the // argument in the field. numberSetIL.Emit(OpCodes.Ldarg_0); numberSetIL.Emit(OpCodes.Ldarg_1); numberSetIL.Emit(OpCodes.Stfld, fbNumber); numberSetIL.Emit(OpCodes.Ret); // Last, map the "get" and "set" accessor methods to the // PropertyBuilder. The property is now complete. pbNumber.SetGetMethod(mbNumberGetAccessor); pbNumber.SetSetMethod(mbNumberSetAccessor); // Define a method that accepts an integer argument and returns // the product of that integer and the private field m_number. This // time, the array of parameter types is created on the fly. MethodBuilder meth = tb.DefineMethod( "MyMethod", MethodAttributes.Public, typeof(int), new Type[] { typeof(int) }); ILGenerator methIL = meth.GetILGenerator(); // To retrieve the private instance field, load the instance it // belongs to (argument zero). After loading the field, load the // argument one and then multiply. Return from the method with // the return value (the product of the two numbers) on the // execution stack. methIL.Emit(OpCodes.Ldarg_0); methIL.Emit(OpCodes.Ldfld, fbNumber); methIL.Emit(OpCodes.Ldarg_1); methIL.Emit(OpCodes.Mul); methIL.Emit(OpCodes.Ret); // Finish the type. Type t = tb.CreateType(); // The code can be executed immediately. Start by getting reflection // objects for the method and the property. MethodInfo mi = t.GetMethod("MyMethod"); PropertyInfo pi = t.GetProperty("Number"); // Create an instance of MyDynamicType using the default // constructor. object o1 = Activator.CreateInstance(t); // Display the value of the property, then change it to 127 and // display it again. Use null to indicate that the property // has no index. outputBlock.Text += String.Format("o1.Number: {0}\n", pi.GetValue(o1, null)); pi.SetValue(o1, 127, null); outputBlock.Text += String.Format("o1.Number: {0}\n", pi.GetValue(o1, null)); // Call MyMethod, passing 22, and display the return value, 22 // times 127. Arguments must be passed as an array, even when // there is only one. object[] arguments = { 22 }; outputBlock.Text += String.Format("o1.MyMethod(22): {0}\n", mi.Invoke(o1, arguments)); // Create an instance of MyDynamicType using the constructor // that specifies m_Number. The constructor is identified by // matching the types in the argument array. In this case, // the argument array is created on the fly. Display the // property value. object o2 = Activator.CreateInstance(t, new object[] { 5280 }); outputBlock.Text += String.Format("o2.Number: {0}\n", pi.GetValue(o2, null)); } } /* This code produces the following output: o1.Number: 42 o1.Number: 127 o1.MyMethod(22): 2794 o2.Number: 5280 */
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.



Note: