AssemblyBuilder::AddResourceFile Method (String^, String^, ResourceAttributes)
Adds an existing resource file to this assembly.
Assembly: mscorlib (in mscorlib.dll)
public: void AddResourceFile( String^ name, String^ fileName, ResourceAttributes attribute )
Parameters
- name
-
Type:
System::String^
The logical name 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; the file must be in the same directory as the assembly to which it is added.
- attribute
-
Type:
System.Reflection::ResourceAttributes
The resource attributes.
| 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 if the length of fileName is zero. -or- fileName includes a path. |
| ArgumentNullException | name or fileName is null. |
| FileNotFoundException | If the file fileName is not found. |
| SecurityException | The caller does not have the required permission. |
fileName should not be the same as that of any other persistable module, standalone managed resource, or the standalone manifest file.
Attributes can be specified for the managed resource.
The specified resource file must be in the directory where the assembly will be 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. |
The following code sample demonstrates how to attach a resource file to a dynamically created assembly, using AddResourceFile.
using namespace System; using namespace System::IO; using namespace System::Threading; using namespace System::Reflection; using namespace System::Reflection::Emit; ref class AsmBuilderGetFileDemo { public: static String^ myResourceFileName = "MyResource.txt"; static FileInfo^ CreateResourceFile() { FileInfo^ f = gcnew FileInfo( myResourceFileName ); StreamWriter^ sw = f->CreateText(); sw->WriteLine( "Hello, world!" ); sw->Close(); return f; } static AssemblyBuilder^ BuildDynAssembly() { String^ myAsmFileName = "MyAsm.dll"; AppDomain^ myDomain = Thread::GetDomain(); AssemblyName^ myAsmName = gcnew AssemblyName; myAsmName->Name = "MyDynamicAssembly"; AssemblyBuilder^ myAsmBuilder = myDomain->DefineDynamicAssembly( myAsmName, AssemblyBuilderAccess::RunAndSave ); myAsmBuilder->AddResourceFile( "MyResource", myResourceFileName ); // To confirm that the resource file has been added to the manifest, // we will save the assembly as MyAsm.dll. You can view the manifest // and confirm the presence of the resource file by running // "ildasm MyAsm.dll" from the prompt in the directory where you executed // the compiled code. myAsmBuilder->Save( myAsmFileName ); return myAsmBuilder; } }; int main() { FileStream^ myResourceFS = nullptr; AsmBuilderGetFileDemo::CreateResourceFile(); Console::WriteLine( "The contents of MyResource.txt, via GetFile:" ); AssemblyBuilder^ myAsm = AsmBuilderGetFileDemo::BuildDynAssembly(); try { myResourceFS = myAsm->GetFile( AsmBuilderGetFileDemo::myResourceFileName ); } catch ( NotSupportedException^ ) { Console::WriteLine( "---" ); Console::WriteLine( "System::Reflection::Emit::AssemblyBuilder::GetFile\nis not supported in this SDK build." ); Console::WriteLine( "The file data will now be retrieved directly, via a new FileStream." ); Console::WriteLine( "---" ); myResourceFS = gcnew FileStream( AsmBuilderGetFileDemo::myResourceFileName,FileMode::Open ); } StreamReader^ sr = gcnew StreamReader( myResourceFS,System::Text::Encoding::ASCII ); Console::WriteLine( sr->ReadToEnd() ); sr->Close(); }
when invoked late-bound through mechanisms such as Type::InvokeMember. Associated enumeration: ReflectionPermissionFlag::MemberAccess.
The FileIOPermissionAccess.Read permission is needed to access the resource file resourceFileName.
Available since 1.1
Silverlight
Available since 2.0
