DefineResource Method (String, String, ResourceAttributes)
Collapse the table of content
Expand the table of content

ModuleBuilder.DefineResource Method (String, String, ResourceAttributes)

 

Defines the named managed embedded resource with the given attributes that is to be stored in this module.

Namespace:   System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

public IResourceWriter DefineResource(
	string name,
	string description,
	ResourceAttributes attribute
)

Parameters

name
Type: System.String

The name of the resource. name cannot contain embedded nulls.

description
Type: System.String

The description of the resource.

attribute
Type: System.Reflection.ResourceAttributes

The resource attributes.

Return Value

Type: System.Resources.IResourceWriter

A resource writer for the defined resource.

Exception Condition
ArgumentException

Length of name is zero.

ArgumentNullException

name is null.

InvalidOperationException

This module is transient.

-or-

The containing assembly is not persistable.

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.

System_CAPS_noteNote

Starting with the .NET Framework 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag. (See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework 3.5 or later.

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'");
      }
   }

.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft