次の方法で共有


FieldAttributes 列挙体

フィールドの属性を記述するフラグを指定します。

この列挙体には、メンバ値をビットごとに演算するための FlagsAttribute 属性が含まれています。

<Flags>
<Serializable>
Public Enum FieldAttributes
[C#]
[Flags]
[Serializable]
public enum FieldAttributes
[C++]
[Flags]
[Serializable]
__value public enum FieldAttributes
[JScript]
public
   Flags
 Serializable
enum FieldAttributes

解説

FieldAttributes は、 FieldAccessMask からの値を使用して、属性値のアクセス可能な部分だけマスクをオフにします。たとえば、次のコードは Attributes のパブリック ビットが設定されているかどうかを判断します。

(Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public

FieldAttributes を取得するには、最初に Type クラスを取得します。そして、その Type から FieldInfo を取得します。最後に、 FieldInfo から Attributes を取得します。

列挙値は、フィールドに実装されている属性をビットごとの OR 演算によって組み合わせて表す数値です。

メンバ

メンバ名 説明
Assembly

.NET Compact Framework でもサポート。

アセンブリ全体からフィールドにアクセスできることを指定します。 3
FamANDAssem

.NET Compact Framework でもサポート。

このアセンブリのサブタイプだけがフィールドにアクセスできることを指定します。 2
Family

.NET Compact Framework でもサポート。

型およびサブタイプだけがフィールドにアクセスできることを指定します。 4
FamORAssem

.NET Compact Framework でもサポート。

あらゆる場所にあるサブタイプ、およびアセンブリ全体からフィールドにアクセスできることを指定します。 5
FieldAccessMask

.NET Compact Framework でもサポート。

指定されているフィールドのアクセス レベルを指定します。 7
HasDefault

.NET Compact Framework でもサポート。

フィールドが既定値を持つことを指定します。 32768
HasFieldMarshal

.NET Compact Framework でもサポート。

フィールドがマーシャリング情報を持つことを指定します。 4096
HasFieldRVA

.NET Compact Framework でもサポート。

フィールドが Relative Virtual Address (RVA) を持つことを指定します。RVA は、現在のイメージ内のメソッド本体の場所を、メソッドが存在するイメージ ファイルの先頭からの相対アドレスで表した値です。 256
InitOnly

.NET Compact Framework でもサポート。

フィールドは初期化されるだけで、初期化後のフィールドに書き込むことができないことを指定します。 32
Literal

.NET Compact Framework でもサポート。

フィールドの値がコンパイル時 (静的バインディングまたは事前バインディング) 定数であることを指定します。set アクセサはありません。 64
NotSerialized

.NET Compact Framework でもサポート。

型をリモート処理するときに、フィールドをシリアル化する必要がないことを指定します。 128
PinvokeImpl

.NET Compact Framework でもサポート。

今後使用するために予約されています。 8192
Private

.NET Compact Framework でもサポート。

親の型だけがフィールドにアクセスできることを指定します。 1
PrivateScope

.NET Compact Framework でもサポート。

フィールドを参照できないことを指定します。 0
Public

.NET Compact Framework でもサポート。

このスコープが可視である任意のメンバがフィールドにアクセスできることを指定します。 6
ReservedMask

.NET Compact Framework でもサポート。

予約済み。 38144
RTSpecialName

.NET Compact Framework でもサポート。

共通言語ランタイム (メタデータ内部 API) が名前のエンコードをチェックする必要があることを指定します。 1024
SpecialName

.NET Compact Framework でもサポート。

特別なメソッドを指定します。メソッドが特別である理由は名前で説明します。 512
Static

.NET Compact Framework でもサポート。

フィールドが定義済みの型を表すことを指定します。それ以外の場合は、フィールドが表す型はインスタンスごとに異なります。 16

使用例

[Visual Basic, C#] 次の例では、3 つのフィールドを構築し、 FieldAttributes 値が、正しく定義された場合に表示されます。 FieldAttributes には、下の 3 番目のフィールドで示すように、Public と Literal の両方など、複数の属性を含めることができます。

 
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

Module Module1
    Public Class Myfieldattributes
        Public Shared Sub Main()
            Console.WriteLine(ControlChars.Cr + "Reflection.FieldAttributes")
            Dim Myfieldx As New Myfielda()
            Dim Myfieldy As New Myfieldb()
            Dim Myfieldz As New Myfieldc()

            'Get the Type and FieldInfo for each of the three fields.
            Dim MyTypea As Type
'Use GetType on an instance of a type instead of calling Type.GetType.
            MyTypea = Myfieldx.GetType()
            Dim Myfieldinfoa As FieldInfo = MyTypea.GetField("m_field", _
               BindingFlags.NonPublic Or BindingFlags.Instance)
            Dim MyTypeb As Type = Myfieldy.GetType()
            Dim Myfieldinfob As FieldInfo = MyTypeb.GetField("m_field", _
               BindingFlags.Public Or BindingFlags.Instance)
            Dim MyTypec As Type = Myfieldz.GetType()
            Dim Myfieldinfoc As FieldInfo = MyTypec.GetField("m_field", _
               BindingFlags.Public Or BindingFlags.Static)

            'For the first field,
            'get and display the Name, m_field, and attributes.
            Console.Write(ControlChars.CrLf + "{0} - ", MyTypea.Name)
            Console.Write("{0}; ", Myfieldinfoa.GetValue(Myfieldx))
            Dim Myattributesa As FieldAttributes = Myfieldinfoa.Attributes

            'If the FieldAttributes are exactly defined,
            'display them. Otherwise, describe as not defined.
            If [Enum].IsDefined(GetType(FieldAttributes), Myattributesa) Then
                Console.Write("it has a {0} field attribute.", _
                   Myattributesa.ToString())
            Else
                Console.Write("it is not exactly defined.")
            End If

            'For the second field,
            'get and display the Name, field, and attributes.
            Console.Write(ControlChars.CrLf + "{0} - ", MyTypeb.Name)
            Console.Write("{0}; ", Myfieldinfob.GetValue(Myfieldy))
            Dim Myattributesb As FieldAttributes = Myfieldinfob.Attributes

            'If the FieldAttributes are exactly defined,
            'display them; otherwise, describe as not defined.
            If [Enum].IsDefined(GetType(FieldAttributes), Myattributesb) Then
                Console.Write("it has a {0} field attribute.", _
                   Myattributesb.ToString())
            Else
                Console.Write("it is not exactly defined.")
            End If

            'For the third field,
            'get and display the Name, field, and attributes.
            Console.Write(ControlChars.CrLf + "{0} - ", MyTypec.Name)
            Console.Write("{0}; ", Myfieldinfoc.GetValue(Myfieldz))
            Dim Myattributesc As FieldAttributes = Myfieldinfoc.Attributes

            'If the FieldAttributes are exactly defined,
            'display them; otherwise, describe as not defined.
            If [Enum].IsDefined(GetType(FieldAttributes), Myattributesc) Then
                Console.Write("it has a {0} field attribute.", _
                   Myattributesc.ToString())
            Else
                Console.Write("it is not exactly defined.")
            End If
        End Sub
    End Class
    'Make three fields
    'The first field is private
    Public Class Myfielda
        Private m_field As String = "A private field"

        Public Property Field() As String
            Get
                Return m_field
            End Get
            Set(ByVal Value As String)
                If m_field <> Value Then
                    m_field = Value + "---"
                End If
            End Set
        End Property
    End Class

    'The second field is public.
    Public Class Myfieldb
        Public m_field As String = "B public field"

        Public Property Field() As String
            Get
                Return m_field
            End Get
            Set(ByVal Value As String)
                If m_field <> Value Then
                    m_field = Value
                End If
            End Set
        End Property
    End Class

    'The third field is public and literal, which is not exactly defined.
    Public Class Myfieldc
        Public Const m_field As String = "C constant field"

        Public ReadOnly Property Field() As String
            Get
                Return m_field
            End Get
        End Property
    End Class

End Module

[C#] 
using System;
using System.Reflection;

//Make three fields
//The first field is private
public class Myfielda
{
    private string field = "A private field";
    public string Field{
        get{return field;}
        set{if(field!=value) {field=value + "---";}}
    }
}
//The second field is public
public class Myfieldb
{
    public string field = "B public field";
    public string Field{
        get{return field;}
        set{if(field!=value) {field=value;}}
    }
}
//The third field is public and literal, which is not exactly defined.
public class Myfieldc
{
    public const string field = "C constant field";
    public string Field{
        get{return field;}
    }
}
public class Myfieldattributes
{
    public static int Main()
    {
        Console.WriteLine ("\nReflection.FieldAttributes");
        Myfielda Myfielda = new Myfielda();
        Myfieldb Myfieldb = new Myfieldb();
        Myfieldc Myfieldc = new Myfieldc();

        //Get the Type and FieldInfo for each of the three fields
        Type MyTypea = Type.GetType("Myfielda");
        FieldInfo Myfieldinfoa = MyTypea.GetField("field",
            BindingFlags.NonPublic | BindingFlags.Instance);
        Type MyTypeb = Type.GetType("Myfieldb");
        FieldInfo Myfieldinfob = MyTypeb.GetField("field",
            BindingFlags.Public | BindingFlags.Instance);
        Type MyTypec = Type.GetType("Myfieldc");
        FieldInfo Myfieldinfoc = MyTypec.GetField("field",
            BindingFlags.Public | BindingFlags.Static);

        //For the first field;
        //Get and Display the Name, field, and attributes
        Console.Write ("\n{0} - ", MyTypea.FullName);
        Console.Write ("{0}; ", Myfieldinfoa.GetValue(Myfielda));
        FieldAttributes Myattributesa = Myfieldinfoa.Attributes;

        //If the FieldAttributes is exactly defined,
        // print it out, otherwise say it is not defined
        if (Enum.IsDefined(typeof(FieldAttributes),
            Myattributesa))
            Console.Write ("it has a {0} field attribute.",
                Myattributesa.ToString());
        else
            Console.Write ("it is not exactly defined.");

        //For the second field;
        //Get and Display the Name, field, and attributes
        Console.Write ("\n{0} - ", MyTypeb.FullName);
        Console.Write ("{0}; ", Myfieldinfob.GetValue(Myfieldb));
        FieldAttributes Myattributesb = Myfieldinfob.Attributes;

        //If the FieldAttributes is exactly defined,
        // print it out, otherwise say it is not defined
        if (Enum.IsDefined(typeof(FieldAttributes),
            Myattributesb))
            Console.Write ("it has a {0} field attribute.",
                Myattributesb.ToString());
        else
            Console.Write ("it is not exactly defined.");

        //For the third field;
        //Get and Display the Name, field, and attributes
        Console.Write ("\n{0} - ", MyTypec.FullName);
        Console.Write ("{0}; ", Myfieldinfoc.GetValue(Myfieldc));
        FieldAttributes Myattributesc = Myfieldinfoc.Attributes;

        //If the FieldAttributes is exactly defined,
        // print it out, otherwise say it is not defined
        if (Enum.IsDefined(typeof(FieldAttributes),
            Myattributesc))
            Console.Write ("it has a {0} field attribute.",
                Myattributesc.ToString());
        else
            Console.Write ("it is not exactly defined.");

        return 0;
    }
}

[Visual Basic, C#] このコードによって、次の出力が生成されます。

[Visual Basic, C#] Reflection.FieldAttributes

[Visual Basic, C#] Myfielda - A private field; it has a Private field attribute.

[Visual Basic, C#] Myfieldb - B public field; it has a Public field attribute.

[Visual Basic, C#] Myfieldc - C constant field; it is not exactly defined.

[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Reflection

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

System.Reflection 名前空間