AssemblyBuilder.DefineResourceとは? わかりやすく解説

AssemblyBuilder.DefineResource メソッド (String, String, String)

既定のパブリック リソース属性を使用して、このアセンブリのスタンドアロン マネージ リソースを定義します。

名前空間: System.Reflection.Emit
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Function DefineResource ( _
    name As String, _
    description As String, _
    fileName As String _
) As IResourceWriter
Dim instance As AssemblyBuilder
Dim name As String
Dim description As String
Dim fileName As String
Dim returnValue As IResourceWriter

returnValue = instance.DefineResource(name, description, fileName)
public IResourceWriter DefineResource (
    string name,
    string description,
    string fileName
)
public:
IResourceWriter^ DefineResource (
    String^ name, 
    String^ description, 
    String^ fileName
)
public IResourceWriter DefineResource (
    String name, 
    String description, 
    String fileName
)
public function DefineResource (
    name : String, 
    description : String, 
    fileName : String
) : IResourceWriter

パラメータ

name

リソースの論理名。

description

文章によるリソースの説明。

fileName

論理名を割り当てる対象の物理ファイル名 (.resources ファイル)。パスを含めないでください。

戻り値
指定したリソースの ResourceWriter オブジェクト。

例外例外
例外の種類条件

ArgumentException

name が既に定義されています。

または

アセンブリ内に fileName という名前のファイルがもう 1 つあります。

または

name の長さが 0 です。

または

fileName の長さが 0 です。

または

fileName にパスが含まれています。

ArgumentNullException

name または fileName が null 参照 (Visual Basic では Nothing) です。

SecurityException

呼び出し元に、必要なアクセス許可がありません。

解説解説
使用例使用例
Public Shared Sub Main()
   Dim myAssembly As AssemblyBuilder
   Dim myResourceWriter As IResourceWriter
   myAssembly = CType(CreateAssembly(Thread.GetDomain()).Assembly, AssemblyBuilder)
   
   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")
End Sub 'Main

' Create the callee transient dynamic assembly.
Private Shared Function
 CreateAssembly(myAppDomain As AppDomain) As
 Type
   Dim myAssemblyName As New
 AssemblyName()
   myAssemblyName.Name = "MyEmitAssembly"
   Dim myAssembly As AssemblyBuilder = myAppDomain.DefineDynamicAssembly(myAssemblyName,
 _
                                                            AssemblyBuilderAccess.Save)
   Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule",
 _
                                                            "EmittedModule.mod")
   
   ' Define a public class named "HelloWorld" in the assembly.
   Dim helloWorldClass As TypeBuilder = myModule.DefineType("HelloWorld",
 TypeAttributes.Public)
   ' Define the Display method.
   Dim myMethod As MethodBuilder = helloWorldClass.DefineMethod("Display",
 _
                                 MethodAttributes.Public, GetType(String),
 Nothing)
   
   ' Generate IL for GetGreeting.
   Dim methodIL As ILGenerator = myMethod.GetILGenerator()
   methodIL.Emit(OpCodes.Ldstr, "Display method get called.")
   methodIL.Emit(OpCodes.Ret)
   ' Returns the type HelloWorld.
   Return helloWorldClass.CreateType()
End Function 'CreateAssembly
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());
}   
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());
}
public static void main(String[]
 args)
{
    AssemblyBuilder myAssembly;
    IResourceWriter myResourceWriter;

    myAssembly = (AssemblyBuilder)
        CreateAssembly(System.Threading.Thread.GetDomain()).get_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");
} //main

// Create the callee transient dynamic assembly.
private static Type CreateAssembly(AppDomain
 appDomain)
{
    AssemblyName myAssemblyName = new AssemblyName();
    myAssemblyName.set_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.class.ToType(), 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();
} //CreateAssembly
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
AssemblyBuilder クラス
AssemblyBuilder メンバ
System.Reflection.Emit 名前空間

AssemblyBuilder.DefineResource メソッド (String, String, String, ResourceAttributes)

このアセンブリのスタンドアロン マネージ リソースを定義します。マネージ リソースには属性を指定できます。

名前空間: System.Reflection.Emit
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Function DefineResource ( _
    name As String, _
    description As String, _
    fileName As String, _
    attribute As ResourceAttributes _
) As IResourceWriter
Dim instance As AssemblyBuilder
Dim name As String
Dim description As String
Dim fileName As String
Dim attribute As ResourceAttributes
Dim returnValue As IResourceWriter

returnValue = instance.DefineResource(name, description, fileName, attribute)
public IResourceWriter DefineResource (
    string name,
    string description,
    string fileName,
    ResourceAttributes attribute
)
public:
IResourceWriter^ DefineResource (
    String^ name, 
    String^ description, 
    String^ fileName, 
    ResourceAttributes attribute
)
public IResourceWriter DefineResource (
    String name, 
    String description, 
    String fileName, 
    ResourceAttributes attribute
)
public function DefineResource (
    name : String, 
    description : String, 
    fileName : String, 
    attribute : ResourceAttributes
) : IResourceWriter

パラメータ

name

リソースの論理名。

description

文章によるリソースの説明。

fileName

論理名を割り当てる対象の物理ファイル名 (.resources ファイル)。パスを含めないでください。

attribute

リソース属性。

戻り値
指定したリソースの ResourceWriter オブジェクト。

例外例外
例外の種類条件

ArgumentException

name が既に定義されています。または、同じ fileName のアセンブリが別のファイルにあります。

または

name の長さが 0 です。

または

fileName の長さが 0 です。

または

fileName にパスが含まれています。

ArgumentNullException

name または fileName が null 参照 (Visual Basic では Nothing) です。

SecurityException

呼び出し元に、必要なアクセス許可がありません。

解説解説
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
AssemblyBuilder クラス
AssemblyBuilder メンバ
System.Reflection.Emit 名前空間

AssemblyBuilder.DefineResource メソッド




英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「AssemblyBuilder.DefineResource」の関連用語

AssemblyBuilder.DefineResourceのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



AssemblyBuilder.DefineResourceのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS