AssemblyBuilder.DefineResource Metoda

Definicja

Definiuje autonomiczny zasób zarządzany dla tego zestawu.

Przeciążenia

DefineResource(String, String, String)

Definiuje autonomiczny zasób zarządzany dla tego zestawu z domyślnym atrybutem zasobu publicznego.

DefineResource(String, String, String, ResourceAttributes)

Definiuje autonomiczny zasób zarządzany dla tego zestawu. Atrybuty można określić dla zasobu zarządzanego.

DefineResource(String, String, String)

Definiuje autonomiczny zasób zarządzany dla tego zestawu z domyślnym atrybutem zasobu publicznego.

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

Parametry

name
String

Nazwa logiczna zasobu.

description
String

Tekstowy opis zasobu.

fileName
String

Nazwa pliku fizycznego (plik resources), na który jest mapowana nazwa logiczna. Nie powinno to zawierać ścieżki.

Zwraca

ResourceWriter Obiekt dla określonego zasobu.

Wyjątki

name został wcześniej zdefiniowany.

-lub-

Istnieje inny plik w zestawie o nazwie fileName.

-lub-

Długość name to zero.

-lub-

Długość fileName to zero.

-lub-

fileName zawiera ścieżkę.

name lub fileName ma wartość null.

Obiekt wywołujący nie posiada wymaganych uprawnień.

Przykłady

W poniższym przykładzie użyto DefineResource metody w celu pobrania modułu zapisywania zasobów. W przykładzie użyto składnika zapisywania zasobów do dodania trzech ciągów zasobów.

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 = safe_cast<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.
static Type^ CreateAssembly( AppDomain^ appDomain )
{
   AssemblyName^ myAssemblyName = gcnew 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, String::typeid, nullptr );

   // 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());
}
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());
}
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

' 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

Uwagi

Zasoby szczegółowe można dodawać za pomocą zwracanego ResourceWriter elementu przez wywołanie metody AddResource.

fileName nie powinna być taka sama jak w przypadku każdego innego modułu utrwalalnego, autonomicznego zasobu zarządzanego lub autonomicznego pliku manifestu.

Środowisko uruchomieniowe wywołuje metodę Close podczas zapisywania zestawu dynamicznego.

Uwaga

Począwszy od .NET Framework 2.0 z dodatkiem Service Pack 1, ten element członkowski nie wymaga ReflectionPermission już flagi ReflectionPermissionFlag.ReflectionEmit . (Zobacz Problemy z zabezpieczeniami w emisji odbicia). Aby korzystać z tej funkcji, aplikacja powinna być docelowa dla .NET Framework 3.5 lub nowszej.

Dotyczy

DefineResource(String, String, String, ResourceAttributes)

Definiuje autonomiczny zasób zarządzany dla tego zestawu. Atrybuty można określić dla zasobu zarządzanego.

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

Parametry

name
String

Nazwa logiczna zasobu.

description
String

Tekstowy opis zasobu.

fileName
String

Nazwa pliku fizycznego (plik resources), na który jest mapowana nazwa logiczna. Nie powinno to zawierać ścieżki.

attribute
ResourceAttributes

Atrybuty zasobu.

Zwraca

ResourceWriter Obiekt dla określonego zasobu.

Wyjątki

name został wcześniej zdefiniowany lub jeśli istnieje inny plik w zestawie o nazwie fileName.

-lub-

Długość name to zero.

-lub-

Długość fileName to zero.

-lub-

fileName zawiera ścieżkę.

name lub fileName ma wartość null.

Obiekt wywołujący nie posiada wymaganych uprawnień.

Uwagi

Zasoby szczegółowe można dodawać za pomocą zwracanego ResourceWriter elementu przez wywołanie metody AddResource.

fileName nie powinien być taki sam jak w przypadku innych modułów trwałych, autonomicznych zasobów zarządzanych ani autonomicznego pliku manifestu.

Środowisko uruchomieniowe wywołuje metodę Close podczas zapisywania zestawu dynamicznego.

Uwaga

Począwszy od .NET Framework 2.0 z dodatkiem Service Pack 1, ten element członkowski nie wymaga ReflectionPermission już flagi ReflectionPermissionFlag.ReflectionEmit . (Zobacz Problemy z zabezpieczeniami w emisji odbicia). Aby korzystać z tej funkcji, aplikacja powinna być docelowa dla .NET Framework 3.5 lub nowszej.

Dotyczy