FieldBuilder クラス

定義

フィールドを定義して表します。 このクラスは継承できません。

public ref class FieldBuilder sealed : System::Reflection::FieldInfo, System::Runtime::InteropServices::_FieldBuilder
public ref class FieldBuilder sealed : System::Reflection::FieldInfo
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public sealed class FieldBuilder : System.Reflection.FieldInfo, System.Runtime.InteropServices._FieldBuilder
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class FieldBuilder : System.Reflection.FieldInfo, System.Runtime.InteropServices._FieldBuilder
public sealed class FieldBuilder : System.Reflection.FieldInfo
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
type FieldBuilder = class
    inherit FieldInfo
    interface _FieldBuilder
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type FieldBuilder = class
    inherit FieldInfo
    interface _FieldBuilder
type FieldBuilder = class
    inherit FieldInfo
Public NotInheritable Class FieldBuilder
Inherits FieldInfo
Implements _FieldBuilder
Public NotInheritable Class FieldBuilder
Inherits FieldInfo
継承
FieldBuilder
属性
実装

次の例は、 FieldBuilder クラスの使用方法を示しています。

using System;
using System.Reflection;
using System.Reflection.Emit;

public class FieldBuilder_Sample
{
    private static Type? CreateType()
    {
        // Create an assembly.
        AssemblyName assemName = new AssemblyName();
        assemName.Name = "DynamicAssembly";
        AssemblyBuilder assemBuilder =
                       AssemblyBuilder.DefineDynamicAssembly(assemName, AssemblyBuilderAccess.Run);
        // Create a dynamic module in Dynamic Assembly.
        ModuleBuilder modBuilder = assemBuilder.DefineDynamicModule("DynamicModule");
        // Define a public class named "DynamicClass" in the assembly.
        TypeBuilder typBuilder = modBuilder.DefineType("DynamicClass", TypeAttributes.Public);

        // Define a private String field named "DynamicField" in the type.
        FieldBuilder fldBuilder = typBuilder.DefineField("DynamicField",
            typeof(string), FieldAttributes.Private | FieldAttributes.Static);
        // Create the constructor.
        Type[] constructorArgs = { typeof(String) };
        ConstructorBuilder constructor = typBuilder.DefineConstructor(
           MethodAttributes.Public, CallingConventions.Standard, constructorArgs);
        ILGenerator constructorIL = constructor.GetILGenerator();
        constructorIL.Emit(OpCodes.Ldarg_0);
        ConstructorInfo? superConstructor = typeof(Object).GetConstructor(new Type[0]);
        constructorIL.Emit(OpCodes.Call, superConstructor!);
        constructorIL.Emit(OpCodes.Ldarg_0);
        constructorIL.Emit(OpCodes.Ldarg_1);
        constructorIL.Emit(OpCodes.Stfld, fldBuilder);
        constructorIL.Emit(OpCodes.Ret);

        // Create the DynamicMethod method.
        MethodBuilder methBuilder = typBuilder.DefineMethod("DynamicMethod",
                             MethodAttributes.Public, typeof(String), null);
        ILGenerator methodIL = methBuilder.GetILGenerator();
        methodIL.Emit(OpCodes.Ldarg_0);
        methodIL.Emit(OpCodes.Ldfld, fldBuilder);
        methodIL.Emit(OpCodes.Ret);

        Console.WriteLine($"Name               : {fldBuilder.Name}");
        Console.WriteLine($"DeclaringType      : {fldBuilder.DeclaringType}");
        Console.WriteLine($"Type               : {fldBuilder.FieldType}");
        return typBuilder.CreateType();
    }

    public static void Main()
    {
        Type? dynType = CreateType();
        try
        {
            if (dynType is not null)
            {
                // Create an instance of the "HelloWorld" class.
                Object? helloWorld = Activator.CreateInstance(dynType, new Object[] { "HelloWorld" });
                // Invoke the "DynamicMethod" method of the "DynamicClass" class.
                Object? obj = dynType.InvokeMember("DynamicMethod",
                               BindingFlags.InvokeMethod, null, helloWorld, null);
                Console.WriteLine($"DynamicClass.DynamicMethod returned: \"{obj}\"");
            }
        }
        catch (MethodAccessException e)
        {
            Console.WriteLine($"{e.GetType().Name}: {e.Message}");
        }
    }
}
Imports System.Reflection
Imports System.Reflection.Emit

Public Module FieldBuilder_Sample
   Private Function CreateType() As Type
      ' Create an assembly.
      Dim assemName As New AssemblyName()
      assemName.Name = "DynamicAssembly"
      Dim assemBuilder As AssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemName,
                                                AssemblyBuilderAccess.Run)
      ' Create a dynamic module in Dynamic Assembly.
      Dim modBuilder As ModuleBuilder = assemBuilder.DefineDynamicModule("DynamicModule")
      ' Define a public class named "DynamicClass" in the assembly.
      Dim typBuilder As TypeBuilder = modBuilder.DefineType("DynamicClass", 
                                          TypeAttributes.Public)
      ' Define a private String field named "DynamicField" in the type.
      Dim fldBuilder As FieldBuilder = typBuilder.DefineField("DynamicField",
                  GetType(String), FieldAttributes.Private Or FieldAttributes.Static)
      ' Create the constructor.
      Dim constructorArgs As Type() = {GetType(String)}
      Dim constructor As ConstructorBuilder = 
                  typBuilder.DefineConstructor(MethodAttributes.Public, 
                           CallingConventions.Standard, constructorArgs)
      Dim constructorIL As ILGenerator = constructor.GetILGenerator()
      constructorIL.Emit(OpCodes.Ldarg_0)
      Dim superConstructor As ConstructorInfo = GetType(Object).GetConstructor(New Type() {})
      constructorIL.Emit(OpCodes.Call, superConstructor)
      constructorIL.Emit(OpCodes.Ldarg_0)
      constructorIL.Emit(OpCodes.Ldarg_1)
      constructorIL.Emit(OpCodes.Stfld, fldBuilder)
      constructorIL.Emit(OpCodes.Ret)

      ' Create the DynamicMethod method.
      Dim methBuilder As MethodBuilder = typBuilder.DefineMethod("DynamicMethod", 
                        MethodAttributes.Public, GetType(String), Nothing)
      Dim methodIL As ILGenerator = methBuilder.GetILGenerator()
      methodIL.Emit(OpCodes.Ldarg_0)
      methodIL.Emit(OpCodes.Ldfld, fldBuilder)
      methodIL.Emit(OpCodes.Ret)

      Console.WriteLine($"Name               : {fldBuilder.Name}")
      Console.WriteLine($"DeclaringType      : {fldBuilder.DeclaringType}")
      Console.WriteLine($"Type               : {fldBuilder.FieldType}")
      Return typBuilder.CreateType()
   End Function 

   Public Sub Main()
      Dim dynType As Type = CreateType()
      Try  
        ' Create an instance of the "HelloWorld" class.
         Dim helloWorld As Object = Activator.CreateInstance(dynType, New Object() {"HelloWorld"})
         ' Invoke the "DynamicMethod" method of the "DynamicClass" class.
         Dim obj As Object = dynType.InvokeMember("DynamicMethod", 
                  BindingFlags.InvokeMethod, Nothing, helloWorld, Nothing)
         Console.WriteLine($"DynamicClass.DynamicMethod returned: ""{obj}""")
      Catch e As MethodAccessException
            Console.WriteLine($"{e.GetType().Name}: {e.Message}")
      End Try
   End Sub 
End Module

注釈

DefineFieldDefineInitializedData、またはDefineUninitializedDataを呼び出して、FieldBuilderのインスタンスを取得します。

Note

SetValue メソッドは現在サポートされていません。 回避策として、完了した型を反映して FieldInfo を取得し、 SetValue を呼び出してフィールドの値を設定します。

プロパティ

名前 説明
Attributes

このフィールドの属性を示します。 このプロパティは読み取り専用です。

CustomAttributes

このメンバーのカスタム属性を含むコレクションを取得します。

(継承元 MemberInfo)
DeclaringType

このフィールドを宣言する型の Type オブジェクトへの参照を示します。 このプロパティは読み取り専用です。

FieldHandle

このフィールドの内部メタデータ ハンドルを示します。 このプロパティは読み取り専用です。

FieldType

このフィールドの型を表す Type オブジェクトを示します。 このプロパティは読み取り専用です。

IsAssembly

このフィールドの潜在的な可視性が Assemblyによって記述されているかどうかを示す値を取得します。つまり、フィールドは同じアセンブリ内の他の型に対して最大で表示され、アセンブリ外の派生型には表示されません。

(継承元 FieldInfo)
IsFamily

このフィールドの可視性が Familyによって記述されるかどうかを示す値を取得します。つまり、フィールドはそのクラスおよび派生クラス内でのみ表示されます。

(継承元 FieldInfo)
IsFamilyAndAssembly

このフィールドの可視性が FamANDAssemによって記述されるかどうかを示す値を取得します。つまり、フィールドは派生クラスからアクセスできますが、同じアセンブリ内にある場合にのみアクセスできます。

(継承元 FieldInfo)
IsFamilyOrAssembly

このフィールドの潜在的な可視性が FamORAssemによって記述されるかどうかを示す値を取得します。つまり、フィールドは、どこにいても派生クラス、および同じアセンブリ内のクラスによってアクセスできます。

(継承元 FieldInfo)
IsInitOnly

フィールドをコンストラクターの本体でのみ設定できるかどうかを示す値を取得します。

(継承元 FieldInfo)
IsLiteral

値がコンパイル時に書き込まれ、変更できないかどうかを示す値を取得します。

(継承元 FieldInfo)
IsNotSerialized

このフィールドに NotSerialized 属性があるかどうかを示す値を取得します。

(継承元 FieldInfo)
IsPinvokeImpl

対応する PinvokeImpl 属性が FieldAttributesで設定されているかどうかを示す値を取得します。

(継承元 FieldInfo)
IsPrivate

フィールドがプライベートかどうかを示す値を取得します。

(継承元 FieldInfo)
IsPublic

フィールドがパブリックかどうかを示す値を取得します。

(継承元 FieldInfo)
IsSecurityCritical

現在の信頼レベルで、現在のフィールドがセキュリティ クリティカルかセキュリティ セーフ クリティカルかを示す値を取得します。

(継承元 FieldInfo)
IsSecuritySafeCritical

現在のフィールドが現在の信頼レベルでセキュリティ セーフ クリティカルであるかどうかを示す値を取得します。

(継承元 FieldInfo)
IsSecurityTransparent

現在のフィールドが現在の信頼レベルで透過的かどうかを示す値を取得します。

(継承元 FieldInfo)
IsSpecialName

対応する SpecialName 属性が FieldAttributes 列挙子に設定されているかどうかを示す値を取得します。

(継承元 FieldInfo)
IsStatic

フィールドが静的かどうかを示す値を取得します。

(継承元 FieldInfo)
MemberType

このメンバーがフィールドであることを示す MemberTypes 値を取得します。

(継承元 FieldInfo)
MetadataToken

メタデータ要素を識別する値を取得します。

(継承元 MemberInfo)
Module

このフィールドを含む型が定義されているモジュールを取得します。

Module

現在の MemberInfo によって表されるメンバーを宣言する型が定義されているモジュールを取得します。

(継承元 MemberInfo)
Name

このフィールドの名前を示します。 このプロパティは読み取り専用です。

ReflectedType

このオブジェクトが取得された Type オブジェクトへの参照を示します。 このプロパティは読み取り専用です。

メソッド

名前 説明
Equals(Object)

このインスタンスが指定したオブジェクトと等しいかどうかを示す値を返します。

(継承元 FieldInfo)
GetCustomAttributes(Boolean)

このフィールドに定義されているすべてのカスタム属性を返します。

GetCustomAttributes(Type, Boolean)

指定された型で識別されるこのフィールドに対して定義されているすべてのカスタム属性を返します。

GetCustomAttributesData()

ターゲット メンバーに適用 CustomAttributeData 属性に関するデータを表すオブジェクトの一覧を返します。

(継承元 MemberInfo)
GetHashCode()

このインスタンスのハッシュ コードを返します。

(継承元 FieldInfo)
GetOptionalCustomModifiers()

フィールドのオプションのカスタム修飾子を識別する型の配列を取得します。

(継承元 FieldInfo)
GetRawConstantValue()

コンパイラによってフィールドに関連付けられているリテラル値を返します。

(継承元 FieldInfo)
GetRequiredCustomModifiers()

プロパティの必要なカスタム修飾子を識別する型の配列を取得します。

(継承元 FieldInfo)
GetToken()

このフィールドを表すトークンを返します。

GetType()

クラス フィールドの属性を検出し、フィールド メタデータへのアクセスを提供します。

(継承元 FieldInfo)
GetValue(Object)

指定されたオブジェクトでサポートされているフィールドの値を取得します。

GetValueDirect(TypedReference)

特定のオブジェクトでサポートされているフィールドの値を返します。

(継承元 FieldInfo)
HasSameMetadataDefinitionAs(MemberInfo)

フィールドを定義して表します。 このクラスは継承できません。

(継承元 MemberInfo)
IsDefined(Type, Boolean)

指定した型を持つ属性がフィールドで定義されているかどうかを示します。

MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
SetConstant(Object)

このフィールドの既定値を設定します。

SetCustomAttribute(ConstructorInfo, Byte[])

指定したカスタム属性 BLOB を使用してカスタム属性を設定します。

SetCustomAttribute(CustomAttributeBuilder)

カスタム属性ビルダーを使用してカスタム属性を設定します。

SetMarshal(UnmanagedMarshal)
古い.

フィールドのネイティブ マーシャリングについて説明します。

SetOffset(Int32)

フィールド レイアウトを指定します。

SetValue(Object, Object, BindingFlags, Binder, CultureInfo)

指定したオブジェクトでサポートされているフィールドの値を設定します。

SetValue(Object, Object)

指定したオブジェクトでサポートされているフィールドの値を設定します。

(継承元 FieldInfo)
SetValueDirect(TypedReference, Object)

指定したオブジェクトでサポートされているフィールドの値を設定します。

(継承元 FieldInfo)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

名前 説明
_FieldBuilder.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

一連の名前を対応する一連のディスパッチ識別子に割り当てます。

_FieldBuilder.GetTypeInfo(UInt32, UInt32, IntPtr)

オブジェクトの型情報を取得します。この型情報を使用して、インターフェイスの型情報を取得できます。

_FieldBuilder.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。

_FieldBuilder.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されるプロパティとメソッドへのアクセスを提供します。

_FieldInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

一連の名前を対応する一連のディスパッチ識別子に割り当てます。

(継承元 FieldInfo)
_FieldInfo.GetType()

Type型を表すFieldInfo オブジェクトを取得します。

(継承元 FieldInfo)
_FieldInfo.GetTypeInfo(UInt32, UInt32, IntPtr)

オブジェクトの型情報を取得します。この型情報を使用して、インターフェイスの型情報を取得できます。

(継承元 FieldInfo)
_FieldInfo.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。

(継承元 FieldInfo)
_FieldInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されるプロパティとメソッドへのアクセスを提供します。

(継承元 FieldInfo)
_MemberInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

一連の名前を対応する一連のディスパッチ識別子に割り当てます。

(継承元 MemberInfo)
_MemberInfo.GetType()

Type クラスを表すMemberInfo オブジェクトを取得します。

(継承元 MemberInfo)
_MemberInfo.GetTypeInfo(UInt32, UInt32, IntPtr)

オブジェクトの型情報を取得します。この型情報を使用して、インターフェイスの型情報を取得できます。

(継承元 MemberInfo)
_MemberInfo.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。

(継承元 MemberInfo)
_MemberInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されるプロパティとメソッドへのアクセスを提供します。

(継承元 MemberInfo)

拡張メソッド

名前 説明
GetCustomAttribute(MemberInfo, Type, Boolean)

指定したメンバーに適用される、指定した型のカスタム属性を取得し、必要に応じてそのメンバーの先祖を検査します。

GetCustomAttribute(MemberInfo, Type)

指定したメンバーに適用される、指定した型のカスタム属性を取得します。

GetCustomAttribute<T>(MemberInfo, Boolean)

指定したメンバーに適用される、指定した型のカスタム属性を取得し、必要に応じてそのメンバーの先祖を検査します。

GetCustomAttribute<T>(MemberInfo)

指定したメンバーに適用される、指定した型のカスタム属性を取得します。

GetCustomAttributes(MemberInfo, Boolean)

指定したメンバーに適用されるカスタム属性のコレクションを取得し、必要に応じてそのメンバーの先祖を検査します。

GetCustomAttributes(MemberInfo, Type, Boolean)

指定したメンバーに適用される、指定した型のカスタム属性のコレクションを取得し、必要に応じてそのメンバーの先祖を検査します。

GetCustomAttributes(MemberInfo, Type)

指定したメンバーに適用される、指定した型のカスタム属性のコレクションを取得します。

GetCustomAttributes(MemberInfo)

指定したメンバーに適用されるカスタム属性のコレクションを取得します。

GetCustomAttributes<T>(MemberInfo, Boolean)

指定したメンバーに適用される、指定した型のカスタム属性のコレクションを取得し、必要に応じてそのメンバーの先祖を検査します。

GetCustomAttributes<T>(MemberInfo)

指定したメンバーに適用される、指定した型のカスタム属性のコレクションを取得します。

IsDefined(MemberInfo, Type, Boolean)

指定した型のカスタム属性が指定したメンバーに適用され、必要に応じてその先祖に適用されるかどうかを示します。

IsDefined(MemberInfo, Type)

指定した型のカスタム属性が、指定したメンバーに適用されるかどうかを示します。

適用対象