英語で読む

次の方法で共有


AssemblyName.CodeBase プロパティ

定義

注意事項

AssemblyName.CodeBase and AssemblyName.EscapedCodeBase are obsolete. Using them for loading an assembly is not supported.

アセンブリの場所を表す URL を取得または設定します。

C#
public string? CodeBase { get; set; }
C#
[System.Obsolete("AssemblyName.CodeBase and AssemblyName.EscapedCodeBase are obsolete. Using them for loading an assembly is not supported.", DiagnosticId="SYSLIB0044", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public string? CodeBase { get; set; }
C#
public string CodeBase { get; set; }

プロパティ値

アセンブリの URL の場所を表す文字列。

属性

次の例では、動的アセンブリを出力し、現在のディレクトリに保存します。 アセンブリが作成されると、 プロパティを CodeBase 使用して、アセンブリが保存されるディレクトリを指定します。

C#

using System;
using System.Reflection;
using System.Threading;
using System.IO;
using System.Globalization;
using System.Reflection.Emit;
using System.Configuration.Assemblies;

public class AssemblyName_CodeBase
{
   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();
      // Set the codebase to the physical directory were the assembly resides.
      myAssemblyName.CodeBase = Directory.GetCurrentDirectory();
      // Set the culture information of the assembly to 'English-American'.
      myAssemblyName.CultureInfo = new CultureInfo("en-US");
      // Set the hash algorithm to 'SHA256'.
      myAssemblyName.HashAlgorithm = AssemblyHashAlgorithm.SHA256;
      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];
      }
      // Display the full assembly information to the console.
      if(myAssembly != null)
      {
         Console.WriteLine("\nDisplaying the full assembly name\n");
         Console.WriteLine(myAssembly.GetName().FullName);
      }
   }
}

注釈

アセンブリが読み込まれるときに、 プロパティを使用してこの値を Assembly.CodeBase 取得することもできます。

アセンブリがバイト配列として読み込まれた場合、このプロパティは、読み込まれたアセンブリの場所ではなく、メソッド オーバーロードの Load(Byte[]) 呼び出し元の場所を返します。

注意

プロパティ セットのみで をAssemblyNameCodeBase使用しないでください。 プロパティは CodeBase アセンブリ ID の要素 (名前やバージョンなど) を提供しないため、読み込みは、メソッドから Load 予想されるとおり、ID による読み込み規則に従って行われません。 代わりに、読み込み元ルールを使用してアセンブリが読み込まれます。 読み込み元コンテキストを使用する場合の欠点については、メソッドの Assembly.LoadFrom オーバーロードまたは アセンブリ読み込みのベスト プラクティスに関するページを参照してください。

適用対象

製品 バージョン (廃止)
.NET 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 2.0, 2.1