This documentation is archived and is not being maintained.
AssemblyBuilder.DefineResource Method
.NET Framework 1.1
Defines a standalone managed resource for this assembly.
Overload List
Defines a standalone managed resource for this assembly with the default public resource attribute.
[Visual Basic] Overloads Public Function DefineResource(String, String, String) As IResourceWriter
[C#] public IResourceWriter DefineResource(string, string, string);
[C++] public: IResourceWriter* DefineResource(String*, String*, String*);
[JScript] public function DefineResource(String, String, String) : IResourceWriter;
Defines a standalone managed resource for this assembly. Attributes can be specified for the managed resource.
[Visual Basic] Overloads Public Function DefineResource(String, String, String, ResourceAttributes) As IResourceWriter
[C#] public IResourceWriter DefineResource(string, string, string, ResourceAttributes);
[C++] public: IResourceWriter* DefineResource(String*, String*, String*, ResourceAttributes);
[JScript] public function DefineResource(String, String, String, ResourceAttributes) : IResourceWriter;
Example
[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of DefineResource. For other examples that might be available, see the individual overload topics.
[Visual Basic] 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 'Main ' 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 [C#] 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()); } [C++] #using <mscorlib.dll> 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 = __try_cast<AssemblyBuilder*>(CreateAssembly(Thread::GetDomain())->Assembly); myResourceWriter = myAssembly->DefineResource(S"myResourceFile", S"A sample Resource File", S"MyEmitAssembly.MyResource.resources"); myResourceWriter->AddResource(S"AddResource 1", S"First added resource"); myResourceWriter->AddResource(S"AddResource 2", S"Second added resource"); myResourceWriter->AddResource(S"AddResource 3", S"Third added resource"); myAssembly->DefineVersionInfoResource(S"AssemblySample", "2:0:0:1", S"Microsoft Corporation", S"@Copyright Microsoft Corp. 1990-2001", S".NET is a trademark of Microsoft Corporation"); myAssembly->Save(S"MyEmitAssembly.dll"); } // Create the callee transient dynamic assembly. static Type* CreateAssembly(AppDomain *appDomain) { AssemblyName *myAssemblyName = new AssemblyName(); myAssemblyName->Name = S"MyEmitAssembly"; AssemblyBuilder *myAssembly = appDomain->DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess::Save); ModuleBuilder *myModule = myAssembly->DefineDynamicModule(S"EmittedModule", S"EmittedModule.mod"); // Define a public class named "HelloWorld" in the assembly. TypeBuilder *helloWorldClass = myModule->DefineType(S"HelloWorld", TypeAttributes::Public); // Define the Display method. MethodBuilder *myMethod = helloWorldClass->DefineMethod(S"Display", MethodAttributes::Public, __typeof(String), 0); // Generate IL for GetGreeting. ILGenerator *methodIL = myMethod->GetILGenerator(); methodIL->Emit(OpCodes::Ldstr, S"Display method get called."); methodIL->Emit(OpCodes::Ret); // Returns the type HelloWorld. return(helloWorldClass->CreateType()); }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button in the upper-left corner of the page.
See Also
AssemblyBuilder Class | AssemblyBuilder Members | System.Reflection.Emit Namespace
Show: