AssemblyBuilder.DefineResource Метод
Определение
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
Определяет автономный управляемый ресурс для этой сборки.
Перегрузки
| Имя | Описание |
|---|---|
| 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 file), с которым сопоставляется логическое имя. Это не должно включать путь.
Возвращаемое значение
ResourceWriter Объект для указанного ресурса.
Исключения
name ранее был определен.
–или–
В сборке имеется другой файл с именем fileName.
–или–
Длина name равно нулю.
–или–
Длина fileName равно нулю.
–или–
fileName включает путь.
name или fileName есть null.
Вызывающий объект не имеет требуемого разрешения.
Примеры
В следующем примере метод используется DefineResource для получения записи ресурсов. В примере используется модуль записи ресурсов для добавления трех строк ресурсов.
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
Комментарии
Ресурсы тонкого зерна можно добавить с возвращаемым ResourceWriter путем вызова AddResource.
fileName не должно совпадать с любым другим сохраняемым модулем, автономным управляемым ресурсом или автономным файлом манифеста.
Среда выполнения вызывает Close метод при сохранении динамической сборки.
Применяется к
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 file), с которым сопоставляется логическое имя. Это не должно включать путь.
- attribute
- ResourceAttributes
Атрибуты ресурса.
Возвращаемое значение
ResourceWriter Объект для указанного ресурса.
Исключения
name ранее был определен или имеется другой файл в сборке с именем fileName.
–или–
Длина name равно нулю.
–или–
Длина fileName равно нулю.
–или–
fileName включает путь.
name или fileName есть null.
Вызывающий объект не имеет требуемого разрешения.
Комментарии
Дополнительные ресурсы можно добавить с возвращаемым ResourceWriter путем вызова AddResource.
fileName не должно совпадать с любым другим сохраняемым модулем, автономным управляемым ресурсом или автономным файлом манифеста.
Среда выполнения вызывает Close метод при сохранении динамической сборки.