AssemblyBuilder.DefineResource Method

Definition

Defines a standalone managed resource for this assembly.

Overloads

DefineResource(String, String, String)

Defines a standalone managed resource for this assembly with the default public resource attribute.

DefineResource(String, String, String, ResourceAttributes)

Defines a standalone managed resource for this assembly. Attributes can be specified for the managed resource.

DefineResource(String, String, String)

Defines a standalone managed resource for this assembly with the default public resource attribute.

public System.Resources.IResourceWriter DefineResource (string name, string description, string fileName);

Parameters

name
String

The logical name of the resource.

description
String

A textual description of the resource.

fileName
String

The physical file name (.resources file) to which the logical name is mapped. This should not include a path.

Returns

A ResourceWriter object for the specified resource.

Exceptions

name has been previously defined.

-or-

There is another file in the assembly named fileName.

-or-

The length of name is zero.

-or-

The length of fileName is zero.

-or-

fileName includes a path.

name or fileName is null.

The caller does not have the required permission.

Examples

The following example uses the DefineResource method to get a resource writer. The example uses the resource writer to add three resource strings.

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());
}

Remarks

Fine grain resources can be added with the returned ResourceWriter by calling AddResource.

fileName should not be the same as that of any other persistable module, stand-alone managed resource, or the stand-alone manifest file.

The runtime calls the Close method when the dynamic assembly is saved.

Note

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.

Applies to

.NET Framework 4.8.1 and other versions
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

DefineResource(String, String, String, ResourceAttributes)

Defines a standalone managed resource for this assembly. Attributes can be specified for the managed resource.

public System.Resources.IResourceWriter DefineResource (string name, string description, string fileName, System.Reflection.ResourceAttributes attribute);

Parameters

name
String

The logical name of the resource.

description
String

A textual description of the resource.

fileName
String

The physical file name (.resources file) to which the logical name is mapped. This should not include a path.

attribute
ResourceAttributes

The resource attributes.

Returns

A ResourceWriter object for the specified resource.

Exceptions

name has been previously defined or if there is another file in the assembly named fileName.

-or-

The length of name is zero.

-or-

The length of fileName is zero.

-or-

fileName includes a path.

name or fileName is null.

The caller does not have the required permission.

Remarks

Fine-grain resources can be added with the returned ResourceWriter by calling AddResource.

fileName should not be the same as that of any other persistable module, standalone managed resource, or the standalone manifest file.

The runtime calls the Close method when the dynamic assembly is saved.

Note

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.

Applies to

.NET Framework 4.8.1 and other versions
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