次の方法で共有


AssemblyBuilder.DefineResource メソッド

このアセンブリのスタンドアロン マネージ リソースを定義します。

オーバーロードの一覧

既定のパブリック リソース属性を使用して、このアセンブリのスタンドアロン マネージ リソースを定義します。

[Visual Basic] Overloads Public Function DefineResource(String, String, String) As IResourceWriter

[C#] public IResourceWriter DefineResource(string, string, string);

[C++] public: IResourceWriter* DefineResource(String*, String*, String*);

[JScript] public function DefineResource(String, String, String) : IResourceWriter;

このアセンブリのスタンドアロン マネージ リソースを定義します。マネージ リソースには属性を指定できます。

[Visual Basic] Overloads Public Function DefineResource(String, String, String, ResourceAttributes) As IResourceWriter

[C#] public IResourceWriter DefineResource(string, string, string, ResourceAttributes);

[C++] public: IResourceWriter* DefineResource(String*, String*, String*, ResourceAttributes);

[JScript] public function DefineResource(String, String, String, ResourceAttributes) : IResourceWriter;

使用例

[Visual Basic, C#, C++] メモ   ここでは、DefineResource のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
Public Shared Sub Main()
   Dim myAssembly As AssemblyBuilder
   Dim myResourceWriter As IResourceWriter
   myAssembly = CType(CreateAssembly(Thread.GetDomain()).Assembly, AssemblyBuilder)
   
   myResourceWriter = myAssembly.DefineResource("myResourceFile", "A sample Resource File", _
                                                      "MyEmitAssembly.MyResource.resources")
   myResourceWriter.AddResource("AddResource 1", "First added resource")
   myResourceWriter.AddResource("AddResource 2", "Second added resource")
   myResourceWriter.AddResource("AddResource 3", "Third added resource")
   
   myAssembly.DefineVersionInfoResource("AssemblySample", "2:0:0:1", "Microsoft Corporation", _
         "@Copyright Microsoft Corp. 1990-2001", ".NET is a trademark of Microsoft Corporation")
   myAssembly.Save("MyEmitAssembly.dll")
End Sub 'Main

' Create the callee transient dynamic assembly.
Private Shared Function CreateAssembly(myAppDomain As AppDomain) As Type
   Dim myAssemblyName As New AssemblyName()
   myAssemblyName.Name = "MyEmitAssembly"
   Dim myAssembly As AssemblyBuilder = myAppDomain.DefineDynamicAssembly(myAssemblyName, _
                                                            AssemblyBuilderAccess.Save)
   Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule", _
                                                            "EmittedModule.mod")
   
   ' Define a public class named "HelloWorld" in the assembly.
   Dim helloWorldClass As TypeBuilder = myModule.DefineType("HelloWorld", TypeAttributes.Public)
   ' Define the Display method.
   Dim myMethod As MethodBuilder = helloWorldClass.DefineMethod("Display", _
                                 MethodAttributes.Public, GetType(String), Nothing)
   
   ' Generate IL for GetGreeting.
   Dim methodIL As ILGenerator = myMethod.GetILGenerator()
   methodIL.Emit(OpCodes.Ldstr, "Display method get called.")
   methodIL.Emit(OpCodes.Ret)
   ' Returns the type HelloWorld.
   Return helloWorldClass.CreateType()
End Function 'CreateAssembly

[C#] 
public static void Main() 
{
   AssemblyBuilder myAssembly;
   IResourceWriter myResourceWriter; 
   myAssembly = (AssemblyBuilder)CreateAssembly(Thread.GetDomain()).Assembly;
      
   myResourceWriter = myAssembly.DefineResource("myResourceFile",
      "A sample Resource File", "MyEmitAssembly.MyResource.resources");
   myResourceWriter.AddResource("AddResource 1", "First added resource");
   myResourceWriter.AddResource("AddResource 2", "Second added resource");
   myResourceWriter.AddResource("AddResource 3", "Third added resource");

   myAssembly.DefineVersionInfoResource("AssemblySample", "2:0:0:1",
      "Microsoft Corporation", "@Copyright Microsoft Corp. 1990-2001",
      ".NET is a trademark of Microsoft Corporation");
   myAssembly.Save("MyEmitAssembly.dll");
}

// Create the callee transient dynamic assembly.
private static Type CreateAssembly(AppDomain appDomain) 
{
   AssemblyName myAssemblyName = new AssemblyName();
   myAssemblyName.Name = "MyEmitAssembly";
   AssemblyBuilder myAssembly = appDomain.DefineDynamicAssembly(myAssemblyName, 
      AssemblyBuilderAccess.Save);
   ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule",
      "EmittedModule.mod");

   // Define a public class named "HelloWorld" in the assembly.
   TypeBuilder helloWorldClass = 
      myModule.DefineType("HelloWorld", TypeAttributes.Public);
   // Define the Display method.
   MethodBuilder myMethod = helloWorldClass.DefineMethod("Display", 
      MethodAttributes.Public, typeof(String), null);

   // Generate IL for GetGreeting.
   ILGenerator methodIL = myMethod.GetILGenerator();
   methodIL.Emit(OpCodes.Ldstr, "Display method get called.");
   methodIL.Emit(OpCodes.Ret);

   // Returns the type HelloWorld.
   return(helloWorldClass.CreateType());
}   

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Resources;

/*
   The following program demonstrates the 'DefineResource' and 'DefineVersionInfoResource' 
   methods of 'AssemblyBuilder' class. It builds an assembly and a resource file at runtime.
   The unmanaged version information like product, product version, Company, Copyright, 
   trademark are defined with 'DefineVersionInfoResource' method.
*/

static Type* CreateAssembly(AppDomain *appDomain);

int main()
{
   AssemblyBuilder *myAssembly;
   IResourceWriter *myResourceWriter; 
   myAssembly = __try_cast<AssemblyBuilder*>(CreateAssembly(Thread::GetDomain())->Assembly);

   myResourceWriter = myAssembly->DefineResource(S"myResourceFile",
      S"A sample Resource File", S"MyEmitAssembly.MyResource.resources");
   myResourceWriter->AddResource(S"AddResource 1", S"First added resource");
   myResourceWriter->AddResource(S"AddResource 2", S"Second added resource");
   myResourceWriter->AddResource(S"AddResource 3", S"Third added resource");

   myAssembly->DefineVersionInfoResource(S"AssemblySample", "2:0:0:1",
      S"Microsoft Corporation", S"@Copyright Microsoft Corp. 1990-2001",
      S".NET is a trademark of Microsoft Corporation");
   myAssembly->Save(S"MyEmitAssembly.dll");
}

// Create the callee transient dynamic assembly.
static Type* CreateAssembly(AppDomain *appDomain) 
{
   AssemblyName *myAssemblyName = new AssemblyName();
   myAssemblyName->Name = S"MyEmitAssembly";
   AssemblyBuilder *myAssembly = appDomain->DefineDynamicAssembly(myAssemblyName, 
      AssemblyBuilderAccess::Save);
   ModuleBuilder *myModule = myAssembly->DefineDynamicModule(S"EmittedModule",
      S"EmittedModule.mod");

   // Define a public class named "HelloWorld" in the assembly.
   TypeBuilder *helloWorldClass = 
      myModule->DefineType(S"HelloWorld", TypeAttributes::Public);
   // Define the Display method.
   MethodBuilder *myMethod = helloWorldClass->DefineMethod(S"Display", 
      MethodAttributes::Public, __typeof(String), 0);

   // Generate IL for GetGreeting.
   ILGenerator *methodIL = myMethod->GetILGenerator();
   methodIL->Emit(OpCodes::Ldstr, S"Display method get called.");
   methodIL->Emit(OpCodes::Ret);

   // Returns the type HelloWorld.
   return(helloWorldClass->CreateType());
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

AssemblyBuilder クラス | AssemblyBuilder メンバ | System.Reflection.Emit 名前空間