Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Defines a managed embedded resource to be stored in this module.
DefineResource(String, String) |
Defines the named managed embedded resource to be stored in this module. |
DefineResource(String, String, ResourceAttributes) |
Defines the named managed embedded resource with the given attributes that is to be stored in this module. |
Defines the named managed embedded resource to be stored in this module.
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
The name of the resource. name
cannot contain embedded nulls.
The description of the resource.
A resource writer for the defined resource.
Length of name
is zero.
name
is null.
This module is transient.
-or-
The containing assembly is not persistable.
The following example illustrates the use of DefineResource
to add an external resource to the current ModuleBuilder.
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
The caller must not call the ResourceWriter.Generate()
and ResourceWriter.Close()
methods, because these methods are called by ModuleBuilder.Save
when the dynamic assembly is written to disk.
Use this method to embed a managed resource. To embed a manifest resource blob, use the DefineManifestResource method. For a summary of embedding and linking managed resources and manifest resource blobs, see the DefineManifestResource method.
Product | Versions |
---|---|
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Defines the named managed embedded resource with the given attributes that is to be stored in this module.
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
The name of the resource. name
cannot contain embedded nulls.
The description of the resource.
The resource attributes.
A resource writer for the defined resource.
Length of name
is zero.
name
is null.
This module is transient.
-or-
The containing assembly is not persistable.
The following example illustrates the use of DefineResource to add an external resource to the current ModuleBuilder.
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
The caller must not call the ResourceWriter.Generate()
and ResourceWriter.Close()
methods, because these methods are called by ModuleBuilder.Save
when the dynamic assembly is written to disk.
Use this method to embed a managed resource. To embed a manifest resource blob, use the DefineManifestResource method. For a summary of embedding and linking managed resources and manifest resource blobs, see the DefineManifestResource method.
Product | Versions |
---|---|
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in