Partager via


AssemblyName Constructor

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Initializes a new instance of the AssemblyName class.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Sub New
public AssemblyName()

Examples

The following example creates an AssemblyName and then uses it to create a dynamic assembly.

Imports System.Reflection
Imports System.Threading
Imports System.Reflection.Emit

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Create a dynamic assembly with name 'MyAssembly' and build version '1.0.0.2001'.
      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "MyAssembly"
      myAssemblyName.Version = New Version("1.0.0.2001")

      ' Get the assembly builder from the application domain associated with 
      ' the current thread.
      Dim myAssemblyBuilder As AssemblyBuilder = _
         AppDomain.CurrentDomain.DefineDynamicAssembly(myAssemblyName, _
            AssemblyBuilderAccess.Run)

      ' Create a dynamic module in the assembly.
      Dim myModuleBuilder As ModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule")
      ' Create a type in the module.
      Dim myTypeBuilder As TypeBuilder = myModuleBuilder.DefineType("MyType", _
         TypeAttributes.Public)
      ' Create a method called 'MyMethod'.
      Dim myMethodBuilder As MethodBuilder = myTypeBuilder.DefineMethod("MyMethod", _
         MethodAttributes.Public Or MethodAttributes.HideBySig Or MethodAttributes.Static, _
         GetType(String), Nothing)
      Dim myILGenerator As ILGenerator = myMethodBuilder.GetILGenerator()
      ' Load a string.
      myILGenerator.Emit(OpCodes.Ldstr, "Hello World!")
      ' Return the string.
      myILGenerator.Emit(OpCodes.Ret)

      ' End the creation of the type.
      Dim t As Type = myTypeBuilder.CreateType()

      ' Call the static method.
      Dim result As Object = t.InvokeMember("MyMethod", _
         BindingFlags.InvokeMethod Or BindingFlags.Public Or BindingFlags.Static, _
         Type.DefaultBinder, Nothing, Nothing)
      outputBlock.Text &= String.Format("MyMethod() returned '{0}'." & vbLf, result)

      ' Create an instance of the class.
      Dim obj As Object = Activator.CreateInstance(t)

   End Sub 
End Class 

' This example produces the following output:
'
'MyMethod() returned 'Hello World!'.
using System;
using System.Reflection;
using System.Reflection.Emit;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Create a dynamic assembly with name 'MyAssembly' and build version '1.0.0.2001'.
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "MyAssembly";
      myAssemblyName.Version = new Version("1.0.0.2001");

      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = 
         AppDomain.CurrentDomain.DefineDynamicAssembly(myAssemblyName, 
            AssemblyBuilderAccess.Run);

      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule");
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType", 
         TypeAttributes.Public);
      // Create a method called 'MyMethod'.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("MyMethod", 
         MethodAttributes.Public | MethodAttributes.HideBySig |
         MethodAttributes.Static, typeof(string), null);
      // Get the Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Load a string.
      myILGenerator.Emit(OpCodes.Ldstr, "Hello World!");
      // Return the string.
      myILGenerator.Emit(OpCodes.Ret);

      // End the creation of the type.
      Type t = myTypeBuilder.CreateType();

      // Call the static method.
      object result = t.InvokeMember("MyMethod", 
         BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, 
         Type.DefaultBinder, null, null);
      outputBlock.Text += String.Format("MyMethod() returned '{0}'.\n", result);

      // Create an instance of the class.
      object obj = Activator.CreateInstance(t);
   }
}

/* This example produces the following output:

MyMethod() returned 'Hello World!'.
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.