AssemblyBuilder.DefineResource 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義這個組件的獨立受管理資源。
多載
DefineResource(String, String, String) |
使用預設公用資源屬性定義這個組件的獨立受管理資源。 |
DefineResource(String, String, String, ResourceAttributes) |
定義這個組件的獨立受管理資源。 Managed 資源可指定屬性。 |
DefineResource(String, String, String)
使用預設公用資源屬性定義這個組件的獨立受管理資源。
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
參數
- name
- String
資源的邏輯名稱。
- description
- String
資源的文字描述。
- fileName
- String
邏輯名稱對應到實體檔案名稱 (.resources 檔案)。 這應該不包含路徑。
傳回
所指定資源的 ResourceWriter 物件。
例外狀況
之前已定義過 name
。
-或-
組件中另有一個檔案名為 fileName
。
-或-
name
的長度為零。
-或-
fileName
的長度為零。
-或-
fileName
包含路徑。
name
或 fileName
為 null
。
呼叫端沒有必要的權限。
範例
下列範例會 DefineResource 使用 方法來取得資源寫入器。 此範例會使用資源寫入器來新增三個資源字串。
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
備註
藉由呼叫 AddResource,即可使用 傳ResourceWriter回的 來新增精細的粒度資源。
fileName
不應該與任何其他可保存模組、獨立受控資源或獨立指令清單檔相同。
運行時間會在儲存動態元件時呼叫 Close 方法。
注意
從 .NET Framework 2.0 Service Pack 1 開始,此成員不再需要 ReflectionPermission 旗ReflectionPermissionFlag.ReflectionEmit標。 (請參閱反映發出中的安全性問題.) 若要使用這項功能,您的應用程式應以 .NET Framework 3.5 或更新版本為目標。
適用於
DefineResource(String, String, String, ResourceAttributes)
定義這個組件的獨立受管理資源。 Managed 資源可指定屬性。
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
參數
- name
- String
資源的邏輯名稱。
- description
- String
資源的文字描述。
- fileName
- String
邏輯名稱對應到實體檔案名稱 (.resources 檔案)。 這應該不包含路徑。
- attribute
- ResourceAttributes
資源屬性。
傳回
所指定資源的 ResourceWriter 物件。
例外狀況
先前已定義過 name
,或組件中是否另有一個檔案名為 fileName
。
-或-
name
的長度為零。
-或-
fileName
的長度為零。
-或-
fileName
包含路徑。
name
或 fileName
為 null
。
呼叫端沒有必要的權限。
備註
您可以藉由呼叫 AddResource,透過傳回ResourceWriter的 來新增細微的資源。
fileName
不應該與任何其他可保存模組、獨立受控資源或獨立指令清單檔相同。
運行時間會在儲存動態元件時呼叫 Close 方法。
注意
從 .NET Framework 2.0 Service Pack 1 開始,此成員不再需要 ReflectionPermission 旗ReflectionPermissionFlag.ReflectionEmit標。 (請參閱反映發出中的安全性問題.) 若要使用這項功能,您的應用程式應以 .NET Framework 3.5 或更新版本為目標。