ModuleBuilder.DefineResource 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义要存储在此模块中的托管嵌入资源。
重载
DefineResource(String, String) |
定义要存储在此模块中的已命名托管嵌入资源。 |
DefineResource(String, String, ResourceAttributes) |
用给定的特性定义存储在此模块中的已命名托管嵌入资源。 |
DefineResource(String, String)
定义要存储在此模块中的已命名托管嵌入资源。
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 方法。 有关嵌入和链接托管资源和清单资源 Blob 的摘要,请参阅 DefineManifestResource 方法。
注意
从 .NET Framework 2.0 Service Pack 1 开始,此成员不再需要ReflectionPermission标志ReflectionPermissionFlag.ReflectionEmit。 (请参阅反射发出中的安全问题.) 若要使用此功能,应用程序应面向.NET Framework 3.5 或更高版本。
另请参阅
适用于
DefineResource(String, String, ResourceAttributes)
用给定的特性定义存储在此模块中的已命名托管嵌入资源。
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 方法。 有关嵌入和链接托管资源和清单资源 Blob 的摘要,请参阅 DefineManifestResource 方法。
注意
从 .NET Framework 2.0 Service Pack 1 开始,此成员不再需要ReflectionPermission标志ReflectionPermissionFlag.ReflectionEmit。 (请参阅反射发出中的安全问题.) 若要使用此功能,应用程序应面向.NET Framework 3.5 或更高版本。