英語で読む

次の方法で共有


AssemblyName コンストラクター

定義

AssemblyName クラスの新しいインスタンスを初期化します。

オーバーロード

AssemblyName()

AssemblyName クラスの新しいインスタンスを初期化します。

AssemblyName(String)

指定した表示名を使用して、AssemblyName クラスの新しいインスタンスを初期化します。

AssemblyName()

ソース:
AssemblyName.cs
ソース:
AssemblyName.cs
ソース:
AssemblyName.cs

AssemblyName クラスの新しいインスタンスを初期化します。

C#
public AssemblyName ();

次の例では、 という名前 MyAssembly.exe の動的アセンブリを作成し、ハード ディスクに保存します。 この例を実行した後、 Ildasm.exe (IL 逆アセンブラー) を使用してアセンブリ メタデータを調べることができます。

C#
using System;
using System.Reflection;
using System.Threading;
using System.Reflection.Emit;

public class AssemblyName_Constructor
{
   public static void MakeAssembly(AssemblyName myAssemblyName, string fileName)
   {
      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", fileName);
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
      // Create a method called 'Main'.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.HideBySig |
         MethodAttributes.Static, typeof(void), null);
      // Get the Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Use the utility method to generate the IL instructions that print a string to the console.
      myILGenerator.EmitWriteLine("Hello World!");
      // Generate the 'ret' IL instruction.
      myILGenerator.Emit(OpCodes.Ret);
      // End the creation of the type.
      myTypeBuilder.CreateType();
      // Set the method with name 'Main' as the entry point in the assembly.
      myAssemblyBuilder.SetEntryPoint(myMethodBuilder);
      myAssemblyBuilder.Save(fileName);
   }

   public static void Main()
   {
      // 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");
      MakeAssembly(myAssemblyName, "MyAssembly.exe");

      // Get all the assemblies currently loaded in the application domain.
      Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();

      // Get the dynamic assembly named 'MyAssembly'.
      Assembly myAssembly = null;
      for(int i = 0; i < myAssemblies.Length; i++)
      {
         if(String.Compare(myAssemblies[i].GetName().Name, "MyAssembly") == 0)
            myAssembly = myAssemblies[i];
      }
      if(myAssembly != null)
      {
         Console.WriteLine("\nDisplaying the assembly name\n");
         Console.WriteLine(myAssembly);
      }
   }
}

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

AssemblyName(String)

ソース:
AssemblyName.cs
ソース:
AssemblyName.cs
ソース:
AssemblyName.cs

指定した表示名を使用して、AssemblyName クラスの新しいインスタンスを初期化します。

C#
public AssemblyName (string assemblyName);

パラメーター

assemblyName
String

FullName プロパティによって返される、アセンブリの表示名。

例外

assemblyNamenullです。

assemblyName は長さゼロの文字列です。

参照アセンブリは見つからない、または読み込めませんでした。

注: .NET for Windows ストア アプリ または ポータブル クラス ライブラリでは、代わりに基底クラスの例外 IOExceptionをキャッチします。

次の例では、表示名から の AssemblyName インスタンスを作成します。 表示名の個々の要素は、オブジェクトのプロパティとしてコンソールに AssemblyName 出力されます。

C#
using System;
using System.Reflection;

public class AssemblyNameDemo
{
   public static void Main()
   {
      // Create an AssemblyName, specifying the display name, and then
      // print the properties.
      AssemblyName myAssemblyName =
         new AssemblyName("Example, Version=1.0.0.2001, Culture=en-US, PublicKeyToken=null");
      Console.WriteLine("Name: {0}", myAssemblyName.Name);
      Console.WriteLine("Version: {0}", myAssemblyName.Version);
      Console.WriteLine("CultureInfo: {0}", myAssemblyName.CultureInfo);
      Console.WriteLine("FullName: {0}", myAssemblyName.FullName);
   }
}
/* This code example produces output similar to the following:

Name: Example
Version: 1.0.0.2001
CultureInfo: en-US
FullName: Example, Version=1.0.0.2001, Culture=en-US, PublicKeyToken=null
 */

注釈

指定された assemblyName が解析され、新しい AssemblyName の適切なフィールドが表示名の値で初期化されます。 これは、表示名を解析する推奨される方法です。 表示名を解析する独自のコードを記述することはお勧めしません。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0