AssemblyBuilder.DefineResource 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义此程序集的独立托管资源。
重载
DefineResource(String, String, String) |
使用默认公共资源属性,为此程序集定义一个独立托管资源。 |
DefineResource(String, String, String, ResourceAttributes) |
定义此程序集的独立托管资源。 可以为托管资源指定的属性。 |
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 标志。 (请参阅 Reflection Emit.) 中的安全问题若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。
适用于
DefineResource(String, String, String, ResourceAttributes)
定义此程序集的独立托管资源。 可以为托管资源指定的属性。
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 标志。 (请参阅 Reflection Emit.) 中的安全问题若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。