AssemblyBuilder.AddResourceFile 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將現有的資源檔加入這個組件。
多載
AddResourceFile(String, String) |
將現有的資源檔加入這個組件。 |
AddResourceFile(String, String, ResourceAttributes) |
將現有的資源檔加入這個組件。 |
AddResourceFile(String, String)
將現有的資源檔加入這個組件。
public:
void AddResourceFile(System::String ^ name, System::String ^ fileName);
public void AddResourceFile (string name, string fileName);
member this.AddResourceFile : string * string -> unit
Public Sub AddResourceFile (name As String, fileName As String)
參數
- name
- String
資源的邏輯名稱。
- fileName
- String
邏輯名稱對應到實體檔案名稱 (.resources 檔案)。 這不應該包含路徑;該檔案必須與要加入該檔案的組件位於相同目錄中。
例外狀況
之前已定義過 name
。
-或-
組件中另有一個檔案名為 fileName
。
-或-
name
的長度為零。
-或-
fileName
的長度為零,或如果 fileName
包含路徑。
name
或 fileName
為 null
。
找不到檔案 fileName
。
呼叫端沒有必要的權限。
範例
下列程式代碼範例示範如何使用 將資源檔附加至動態建立的元件 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();
}
using System;
using System.IO;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
class AsmBuilderGetFileDemo
{
private static string myResourceFileName = "MyResource.txt";
private static FileInfo CreateResourceFile()
{
FileInfo f = new FileInfo(myResourceFileName);
StreamWriter sw = f.CreateText();
sw.WriteLine("Hello, world!");
sw.Close();
return f;
}
private static AssemblyBuilder BuildDynAssembly()
{
string myAsmFileName = "MyAsm.dll";
AppDomain myDomain = Thread.GetDomain();
AssemblyName myAsmName = new 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;
}
public static void Main()
{
FileStream myResourceFS = null;
CreateResourceFile();
Console.WriteLine("The contents of MyResource.txt, via GetFile:");
AssemblyBuilder myAsm = BuildDynAssembly();
try
{
myResourceFS = myAsm.GetFile(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 = new FileStream(myResourceFileName,
FileMode.Open);
}
StreamReader sr = new StreamReader(myResourceFS, System.Text.Encoding.ASCII);
Console.WriteLine(sr.ReadToEnd());
sr.Close();
}
}
Imports System.IO
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
_
Class AsmBuilderGetFileDemo
Private Shared myResourceFileName As String = "MyResource.txt"
Private Shared Function CreateResourceFile() As FileInfo
Dim f As New FileInfo(myResourceFileName)
Dim sw As StreamWriter = f.CreateText()
sw.WriteLine("Hello, world!")
sw.Close()
Return f
End Function 'CreateResourceFile
Private Shared Function BuildDynAssembly() As AssemblyBuilder
Dim myAsmFileName As String = "MyAsm.dll"
Dim myDomain As AppDomain = Thread.GetDomain()
Dim myAsmName As New AssemblyName()
myAsmName.Name = "MyDynamicAssembly"
Dim myAsmBuilder As AssemblyBuilder = 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
End Function 'BuildDynAssembly
Public Shared Sub Main()
Dim myResourceFS As FileStream = Nothing
CreateResourceFile()
Console.WriteLine("The contents of MyResource.txt, via GetFile:")
Dim myAsm As AssemblyBuilder = BuildDynAssembly()
Try
myResourceFS = myAsm.GetFile(myResourceFileName)
Catch nsException As NotSupportedException
Console.WriteLine("---")
Console.WriteLine("System.Reflection.Emit.AssemblyBuilder.GetFile is not supported " + _
"in this SDK build.")
Console.WriteLine("The file data will now be retrieved directly, via a new FileStream.")
Console.WriteLine("---")
myResourceFS = New FileStream(myResourceFileName, FileMode.Open)
End Try
Dim sr As New StreamReader(myResourceFS, System.Text.Encoding.ASCII)
Console.WriteLine(sr.ReadToEnd())
sr.Close()
End Sub
End Class
備註
fileName
不應該與任何其他可保存模組、獨立受控資源或獨立指令清單檔相同。
檔案中的Managed資源會假設為公用。
指定的資源檔案必須位於將儲存元件的目錄中。
注意
從 .NET Framework 2.0 Service Pack 1 開始,此成員不再需要 ReflectionPermission 旗ReflectionPermissionFlag.ReflectionEmit標。 (請參閱反映發出.) 的安全性問題 若要使用這項功能,您的應用程式應以 .NET Framework 3.5 或更新版本為目標。
適用於
AddResourceFile(String, String, ResourceAttributes)
將現有的資源檔加入這個組件。
public:
void AddResourceFile(System::String ^ name, System::String ^ fileName, System::Reflection::ResourceAttributes attribute);
public void AddResourceFile (string name, string fileName, System.Reflection.ResourceAttributes attribute);
member this.AddResourceFile : string * string * System.Reflection.ResourceAttributes -> unit
Public Sub AddResourceFile (name As String, fileName As String, attribute As ResourceAttributes)
參數
- name
- String
資源的邏輯名稱。
- fileName
- String
邏輯名稱對應到實體檔案名稱 (.resources 檔案)。 這不應該包含路徑;該檔案必須與要加入該檔案的組件位於相同目錄中。
- attribute
- ResourceAttributes
資源屬性。
例外狀況
之前已定義過 name
。
-或-
組件中另有一個檔案名為 fileName
。
-或-
name
長度為零,或如果 fileName
長度為零。
-或-
fileName
包含路徑。
name
或 fileName
為 null
。
找不到 fileName
檔案。
呼叫端沒有必要的權限。
範例
下列程式代碼範例示範如何使用 將資源檔附加至動態建立的元件 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();
}
using System;
using System.IO;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
class AsmBuilderGetFileDemo
{
private static string myResourceFileName = "MyResource.txt";
private static FileInfo CreateResourceFile()
{
FileInfo f = new FileInfo(myResourceFileName);
StreamWriter sw = f.CreateText();
sw.WriteLine("Hello, world!");
sw.Close();
return f;
}
private static AssemblyBuilder BuildDynAssembly()
{
string myAsmFileName = "MyAsm.dll";
AppDomain myDomain = Thread.GetDomain();
AssemblyName myAsmName = new 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;
}
public static void Main()
{
FileStream myResourceFS = null;
CreateResourceFile();
Console.WriteLine("The contents of MyResource.txt, via GetFile:");
AssemblyBuilder myAsm = BuildDynAssembly();
try
{
myResourceFS = myAsm.GetFile(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 = new FileStream(myResourceFileName,
FileMode.Open);
}
StreamReader sr = new StreamReader(myResourceFS, System.Text.Encoding.ASCII);
Console.WriteLine(sr.ReadToEnd());
sr.Close();
}
}
Imports System.IO
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
_
Class AsmBuilderGetFileDemo
Private Shared myResourceFileName As String = "MyResource.txt"
Private Shared Function CreateResourceFile() As FileInfo
Dim f As New FileInfo(myResourceFileName)
Dim sw As StreamWriter = f.CreateText()
sw.WriteLine("Hello, world!")
sw.Close()
Return f
End Function 'CreateResourceFile
Private Shared Function BuildDynAssembly() As AssemblyBuilder
Dim myAsmFileName As String = "MyAsm.dll"
Dim myDomain As AppDomain = Thread.GetDomain()
Dim myAsmName As New AssemblyName()
myAsmName.Name = "MyDynamicAssembly"
Dim myAsmBuilder As AssemblyBuilder = 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
End Function 'BuildDynAssembly
Public Shared Sub Main()
Dim myResourceFS As FileStream = Nothing
CreateResourceFile()
Console.WriteLine("The contents of MyResource.txt, via GetFile:")
Dim myAsm As AssemblyBuilder = BuildDynAssembly()
Try
myResourceFS = myAsm.GetFile(myResourceFileName)
Catch nsException As NotSupportedException
Console.WriteLine("---")
Console.WriteLine("System.Reflection.Emit.AssemblyBuilder.GetFile is not supported " + _
"in this SDK build.")
Console.WriteLine("The file data will now be retrieved directly, via a new FileStream.")
Console.WriteLine("---")
myResourceFS = New FileStream(myResourceFileName, FileMode.Open)
End Try
Dim sr As New StreamReader(myResourceFS, System.Text.Encoding.ASCII)
Console.WriteLine(sr.ReadToEnd())
sr.Close()
End Sub
End Class
備註
fileName
不應該與任何其他可保存模組、獨立受控資源或獨立指令清單檔相同。
Managed 資源可指定屬性。
指定的資源檔案必須位於將儲存元件的目錄中。
注意
從 .NET Framework 2.0 Service Pack 1 開始,此成員不再需要 ReflectionPermission 旗ReflectionPermissionFlag.ReflectionEmit標。 (請參閱反映發出.) 的安全性問題 若要使用這項功能,您的應用程式應以 .NET Framework 3.5 或更新版本為目標。