共用方式為


ModuleBuilder.DefineResource 方法

定義

定義要儲存於這個模組中的 Managed 內嵌資源。

多載

DefineResource(String, String)

定義要儲存於這個模組中的具名 Managed 內嵌資源。

DefineResource(String, String, ResourceAttributes)

以指定的屬性定義要儲存於這個模組中的具名 Managed 內嵌資源。

DefineResource(String, String)

定義要儲存於這個模組中的具名 Managed 內嵌資源。

public:
 System::Resources::IResourceWriter ^ DefineResource(System::String ^ name, System::String ^ description);
public System.Resources.IResourceWriter DefineResource (string name, string description);
member this.DefineResource : string * string -> System.Resources.IResourceWriter
Public Function DefineResource (name As String, description As String) As IResourceWriter

參數

name
String

資源名稱。 name 不能包含內嵌的 null。

description
String

資源的描述。

傳回

定義之資源的資源寫入器。

例外狀況

name 的長度為零。

name 為 null。

這個模組是暫時性的。

-或-

包含的組件不是保存性的。

範例

下列範例說明如何使用 DefineResource 將外部資源新增至目前的 ModuleBuilder

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Resources;
public ref class CodeGenerator
{
public:
   CodeGenerator()
   {
      
      // Get the current application domain for the current thread.
      AppDomain^ currentDomain = AppDomain::CurrentDomain;
      AssemblyName^ myAssemblyName = gcnew AssemblyName;
      myAssemblyName->Name = "TempAssembly";
      
      // Define 'TempAssembly' assembly in the current application domain.
      AssemblyBuilder^ myAssemblyBuilder = currentDomain->DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess::RunAndSave );
      
      // Define 'TempModule' module in 'TempAssembly' assembly.
      ModuleBuilder^ myModuleBuilder = myAssemblyBuilder->DefineDynamicModule( "TempModule", "TempModule.netmodule", true );
      
      // Define the managed embedded resource, 'MyResource' in 'TempModule'.
      IResourceWriter^ myResourceWriter = myModuleBuilder->DefineResource( "MyResource.resource", "Description" );
      
      // Add resources to the resource writer.
      myResourceWriter->AddResource( "String 1", "First String" );
      myResourceWriter->AddResource( "String 2", "Second String" );
      myResourceWriter->AddResource( "String 3", "Third String" );
      myAssemblyBuilder->Save( "MyAssembly.dll" );
   }

};

int main()
{
   CodeGenerator^ myGenerator = gcnew CodeGenerator;
   Console::WriteLine( "A resource named 'MyResource.resource' has been created and can be viewed in the 'MyAssembly.dll'" );
}
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Resources;

   public class CodeGenerator
   {
      public CodeGenerator()
      {
         // Get the current application domain for the current thread.
         AppDomain currentDomain = AppDomain.CurrentDomain;

         AssemblyName myAssemblyName = new AssemblyName();
         myAssemblyName.Name = "TempAssembly";

         // Define 'TempAssembly' assembly in the current application domain.
         AssemblyBuilder myAssemblyBuilder =
            currentDomain.DefineDynamicAssembly
                        (myAssemblyName, AssemblyBuilderAccess.RunAndSave);
         // Define 'TempModule' module in 'TempAssembly' assembly.
         ModuleBuilder myModuleBuilder =
            myAssemblyBuilder.DefineDynamicModule("TempModule",
                                       "TempModule.netmodule",true);
         // Define the managed embedded resource, 'MyResource' in 'TempModule'.
         IResourceWriter myResourceWriter =
               myModuleBuilder.DefineResource("MyResource.resource","Description");
         // Add resources to the resource writer.
         myResourceWriter.AddResource("String 1", "First String");
         myResourceWriter.AddResource("String 2", "Second String");
         myResourceWriter.AddResource("String 3", "Third String");
         myAssemblyBuilder.Save("MyAssembly.dll");
      }
   }

   public class CallerClass
   {
      public static void Main()
      {
         CodeGenerator myGenerator = new CodeGenerator();
         Console.WriteLine("A resource named 'MyResource.resource'"
                +" has been created and can be viewed  in the 'MyAssembly.dll'");
      }
   }
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Resources

Public Class CodeGenerator
   
   Public Sub New()
      ' Get the current application domain for the current thread.
      Dim currentDomain As AppDomain = AppDomain.CurrentDomain

      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "TempAssembly"
      
      ' Define 'TempAssembly' assembly in the current application domain.
      Dim myAssemblyBuilder As AssemblyBuilder = currentDomain.DefineDynamicAssembly _
                                       (myAssemblyName, AssemblyBuilderAccess.RunAndSave)
      ' Define 'TempModule' module in 'TempAssembly' assembly.
      Dim myModuleBuilder As ModuleBuilder = myAssemblyBuilder.DefineDynamicModule _
                                             ("TempModule", "TempModule.netmodule", True)
      ' Define the managed embedded resource, 'MyResource' in 'TempModule'.
      Dim myResourceWriter As IResourceWriter = myModuleBuilder.DefineResource _
                                                   ("MyResource.resource", "Description")
      ' Add resources to the resource writer.
      myResourceWriter.AddResource("String 1", "First String")
      myResourceWriter.AddResource("String 2", "Second String")
      myResourceWriter.AddResource("String 3", "Third String")
      myAssemblyBuilder.Save("MyAssembly.dll")

   End Sub
End Class

Public Class CallerClass
   
   Public Shared Sub Main()
      Dim myGenerator As New CodeGenerator()
      Console.WriteLine("A resource named 'MyResource.resource' has been created and can be" + _
                                                               " viewed  in the 'MyAssembly.dll'")
   End Sub
End Class

備註

呼叫端不得呼叫 ResourceWriter.Generate()ResourceWriter.Close() 方法,因為這些方法會在動態元件寫入磁碟時呼叫 ModuleBuilder.Save

使用此方法來內嵌受控資源。 若要內嵌指令清單資源 Blob,請使用 DefineManifestResource 方法。 如需內嵌和連結 Managed 資源和指令清單資源 Blob 的摘要,請參閱 DefineManifestResource 方法。

注意

從 .NET Framework 2.0 Service Pack 1 開始,此成員不再需要 ReflectionPermissionReflectionPermissionFlag.ReflectionEmit標。 (請參閱反映 Emit.) 中的安全性問題 若要使用此功能,您的應用程式應以 .NET Framework 3.5 或更新版本為目標。

另請參閱

適用於

DefineResource(String, String, ResourceAttributes)

以指定的屬性定義要儲存於這個模組中的具名 Managed 內嵌資源。

public:
 System::Resources::IResourceWriter ^ DefineResource(System::String ^ name, System::String ^ description, System::Reflection::ResourceAttributes attribute);
public System.Resources.IResourceWriter DefineResource (string name, string description, System.Reflection.ResourceAttributes attribute);
member this.DefineResource : string * string * System.Reflection.ResourceAttributes -> System.Resources.IResourceWriter
Public Function DefineResource (name As String, description As String, attribute As ResourceAttributes) As IResourceWriter

參數

name
String

資源名稱。 name 不能包含內嵌的 null。

description
String

資源的描述。

attribute
ResourceAttributes

資源屬性。

傳回

定義之資源的資源寫入器。

例外狀況

name 的長度為零。

name 為 null。

這個模組是暫時性的。

-或-

包含的組件不是保存性的。

範例

下列範例說明如何使用 DefineResource 將外部資源新增至目前的 ModuleBuilder

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Resources;
public ref class CodeGenerator
{
public:
   CodeGenerator()
   {
      
      // Get the current application domain for the current thread.
      AppDomain^ currentDomain = AppDomain::CurrentDomain;
      AssemblyName^ myAssemblyName = gcnew AssemblyName;
      myAssemblyName->Name = "TempAssembly";
      
      // Define 'TempAssembly' assembly in the current application domain.
      AssemblyBuilder^ myAssemblyBuilder = currentDomain->DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess::RunAndSave );
      
      // Define 'TempModule' module in 'TempAssembly' assembly.
      ModuleBuilder^ myModuleBuilder = myAssemblyBuilder->DefineDynamicModule( "TempModule", "TempModule.netmodule", true );
      
      // Define the managed embedded resource, 'MyResource' in 'TempModule'
      // with the specified attribute.
      IResourceWriter^ writer = myModuleBuilder->DefineResource( "MyResource.resource", "Description", ResourceAttributes::Public );
      
      // Add resources to the resource writer.
      writer->AddResource( "String 1", "First String" );
      writer->AddResource( "String 2", "Second String" );
      writer->AddResource( "String 3", "Third String" );
      myAssemblyBuilder->Save( "MyAssembly.dll" );
   }

};

int main()
{
   CodeGenerator^ myGenerator = gcnew CodeGenerator;
   Console::WriteLine( "A resource named 'MyResource::resource' has been created and can be viewed in the 'MyAssembly.dll'" );
}
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Resources;

   public class CodeGenerator
   {
      public CodeGenerator()
      {
         // Get the current application domain for the current thread.
         AppDomain currentDomain = AppDomain.CurrentDomain;

         AssemblyName myAssemblyName = new AssemblyName();
         myAssemblyName.Name = "TempAssembly";

         // Define 'TempAssembly' assembly in the current application domain.
         AssemblyBuilder myAssemblyBuilder =
            currentDomain.DefineDynamicAssembly
                        (myAssemblyName, AssemblyBuilderAccess.RunAndSave);
         // Define 'TempModule' module in 'TempAssembly' assembly.
         ModuleBuilder myModuleBuilder =
            myAssemblyBuilder.DefineDynamicModule("TempModule",
                                              "TempModule.netmodule",true);
         // Define the managed embedded resource, 'MyResource' in 'TempModule'
         // with the specified attribute.
         IResourceWriter writer =
               myModuleBuilder.DefineResource("MyResource.resource",
                            "Description",ResourceAttributes.Public);
         // Add resources to the resource writer.
         writer.AddResource("String 1", "First String");
         writer.AddResource("String 2", "Second String");
         writer.AddResource("String 3", "Third String");
         myAssemblyBuilder.Save("MyAssembly.dll");
      }
   }

   public class CallerClass
   {
      public static void Main()
      {
         CodeGenerator myGenerator = new CodeGenerator();
         Console.WriteLine("A resource named 'MyResource.resource'"
                +" has been created and can be viewed  in the 'MyAssembly.dll'");
      }
   }
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Resources

Public Class CodeGenerator
   
   Public Sub New()
      ' Get the current application domain for the current thread.
      Dim currentDomain As AppDomain = AppDomain.CurrentDomain

      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "TempAssembly"
      
      ' Define 'TempAssembly' assembly in the current application domain.
      Dim myAssemblyBuilder As AssemblyBuilder = currentDomain.DefineDynamicAssembly _
                                          (myAssemblyName, AssemblyBuilderAccess.RunAndSave)
      ' Define 'TempModule' module in 'TempAssembly' assembly.
      Dim myModuleBuilder As ModuleBuilder = myAssemblyBuilder.DefineDynamicModule _
                                                ("TempModule", "TempModule.netmodule", True)
      ' Define the managed embedded resource, 'MyResource' in 'TempModule'
      ' with the specified attribute.
      Dim writer As IResourceWriter = myModuleBuilder.DefineResource _
                              ("MyResource.resource", "Description", ResourceAttributes.Public)
      ' Add resources to the resource writer.
      writer.AddResource("String 1", "First String")
      writer.AddResource("String 2", "Second String")
      writer.AddResource("String 3", "Third String")
      myAssemblyBuilder.Save("MyAssembly.dll")

   End Sub
End Class

Public Class CallerClass
   
   Public Shared Sub Main()
      Dim myGenerator As New CodeGenerator()
      Console.WriteLine("A resource named 'MyResource.resource' has been created and can be " + _
                                                         "viewed  in the 'MyAssembly.dll'")
   End Sub
End Class

備註

呼叫端不得呼叫 ResourceWriter.Generate()ResourceWriter.Close() 方法,因為這些方法會在動態元件寫入磁碟時呼叫 ModuleBuilder.Save

使用此方法來內嵌受控資源。 若要內嵌指令清單資源 Blob,請使用 DefineManifestResource 方法。 如需內嵌和連結 Managed 資源和指令清單資源 Blob 的摘要,請參閱 DefineManifestResource 方法。

注意

從 .NET Framework 2.0 Service Pack 1 開始,此成員不再需要 ReflectionPermissionReflectionPermissionFlag.ReflectionEmit標。 (請參閱反映 Emit.) 中的安全性問題 若要使用此功能,您的應用程式應以 .NET Framework 3.5 或更新版本為目標。

另請參閱

適用於