Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

AssemblyBuilder::DefineResource Method (String^, String^, String^)

 

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

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

public:
IResourceWriter^ DefineResource(
	String^ name,
	String^ description,
	String^ fileName
)

Parameters

name
Type: System::String^

The logical name of the resource.

description
Type: System::String^

A textual description of the resource.

fileName
Type: System::String^

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

Return Value

Type: System.Resources::IResourceWriter^

A ResourceWriter object for the specified resource.

Exception Condition
ArgumentException

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.

ArgumentNullException

name or fileName is null.

SecurityException

The caller does not have the required permission.

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.

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 uses the DefineResource method to get a resource writer. The example uses the resource writer to add three resource strings.

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 = safe_cast<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.
static Type^ CreateAssembly( AppDomain^ appDomain )
{
   AssemblyName^ myAssemblyName = gcnew 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, String::typeid, nullptr );

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

ReflectionPermission

when invoked late-bound through mechanisms such as Type::InvokeMember. Associated enumeration: ReflectionPermissionFlag::MemberAccess.

FileIOPermission

Write=true or Append=true

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