FieldAttributes 枚举

指定描述字段属性的标志。

此枚举有一个 FlagsAttribute 属性,允许其成员值按位组合。

**命名空间:**System.Reflection
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<FlagsAttribute> _
<ComVisibleAttribute(True)> _
Public Enumeration FieldAttributes
用法
Dim instance As FieldAttributes
[SerializableAttribute] 
[FlagsAttribute] 
[ComVisibleAttribute(true)] 
public enum FieldAttributes
[SerializableAttribute] 
[FlagsAttribute] 
[ComVisibleAttribute(true)] 
public enum class FieldAttributes
/** @attribute SerializableAttribute() */ 
/** @attribute FlagsAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public enum FieldAttributes
SerializableAttribute 
FlagsAttribute 
ComVisibleAttribute(true) 
public enum FieldAttributes

成员

  成员名称 说明
由 .NET Compact Framework 支持 Assembly 指定该字段可由整个程序集访问。 
由 .NET Compact Framework 支持 FamANDAssem 指定该字段只能由此程序集中的子类型访问。 
由 .NET Compact Framework 支持 Family 指定该字段只能由类型和子类型访问。 
由 .NET Compact Framework 支持 FamORAssem 指定该字段可由任意位置的子类型访问,也可由整个程序集访问。 
由 .NET Compact Framework 支持 FieldAccessMask 指定给定字段的访问级别。 
由 .NET Compact Framework 支持 HasDefault 指定该字段具有默认值。 
由 .NET Compact Framework 支持 HasFieldMarshal 指定该字段具有封送处理信息。 
由 .NET Compact Framework 支持 HasFieldRVA 指定该字段具有相对虚拟地址 (RVA)。RVA 是方法体在当前图像中的位置,它是相对于它所在的图像文件的开始的地址。 
由 .NET Compact Framework 支持 InitOnly 指定该字段只能初始化,初始化后不能写入。 
由 .NET Compact Framework 支持 Literal 指定该字段的值是一个编译时(静态或早期绑定)常数。不是设置访问器。 
由 .NET Compact Framework 支持 NotSerialized 指定扩展类型时不必序列化该字段。 
由 .NET Compact Framework 支持 PinvokeImpl 保留供将来使用。 
由 .NET Compact Framework 支持 Private 指定该字段只能由父类型访问。 
由 .NET Compact Framework 支持 PrivateScope 指定该字段不能被引用。 
由 .NET Compact Framework 支持 Public 指定该字段可由任何可看见此范围的成员访问。 
由 .NET Compact Framework 支持 ReservedMask 保留。 
由 .NET Compact Framework 支持 RTSpecialName 指定公共语言运行库(元数据内部 API)应检查名称编码。 
由 .NET Compact Framework 支持 SpecialName 指定一个特殊方法,并用名称说明该方法的特殊性。 
由 .NET Compact Framework 支持 Static 指定该字段表示已定义的类型,否则为每实例方式。 

备注

FieldAttributes 使用 FieldAccessMask 中的值仅屏蔽掉属性值中表示可访问性的部分。例如,下列代码确定 Attributes 是否具有公共位集:

If (Attributes And FieldAttributes.FieldAccessMask) = _
    FieldAttributes.Public Then
if ((Attributes & FieldAttributes.FieldAccessMask) == 
    FieldAttributes.Public)

若要获取 FieldAttributes,请先获取 Type 类。从 Type 获取 FieldInfo。从 FieldInfo 获取 Attributes

枚举值是数字,它表示在字段上实现的属性的按位“或”。

示例

在此示例中,生成了三个字段并显示了 FieldAttributes 值。FieldAttributes 值可以包含一个以上属性,如 PublicLiteral,详见下面第三个字段所示。

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

Public Class Demo
    ' Declare three fields.
    ' The first field is private.
    Private m_field As String = "String A"

    'The second field is public.
    Public Field As String = "String B"

    ' The third field is public and const, hence also static
    ' and literal with a default value.
    Public Const FieldC As String = "String C"

End Class

Module Module1
    Sub Main()
        ' Create an instance of the Demo class.
        Dim d As New Demo()

        Console.WriteLine(vbCrLf & "Reflection.FieldAttributes")

        ' Get a Type object for Demo, and a FieldInfo for each of
        ' the three fields. Use the FieldInfo to display field
        ' name, value for the Demo object in d, and attributes.
        '
        Dim myType As Type = GetType(Demo)

        Dim fiPrivate As FieldInfo = myType.GetField("m_field", _
            BindingFlags.NonPublic Or BindingFlags.Instance)
        DisplayField(d, fiPrivate)

        Dim fiPublic As FieldInfo = myType.GetField("Field", _
            BindingFlags.Public Or BindingFlags.Instance)
        DisplayField(d, fiPublic)

        Dim fiConstant As FieldInfo = myType.GetField("FieldC", _
            BindingFlags.Public Or BindingFlags.Static)
        DisplayField(d, fiConstant)
    End Sub

    Sub DisplayField(ByVal obj As Object, ByVal f As FieldInfo)

        ' Display the field name, value, and attributes.
        '
        Console.WriteLine("{0} = ""{1}""; attributes: {2}", _
            f.Name, f.GetValue(obj), f.Attributes)
    End Sub

End Module

' This code example produces the following output:
'
'm_field = "String A"; attributes: Private
'Field = "String B"; attributes: Public
'FieldC = "String C"; attributes: Public, Static, Literal, HasDefault
using System;
using System.Reflection;

public class Demo
{
    // Make three fields:
    // The first field is private.
    private string m_field = "String A";

    // The second field is public.
    public string Field = "String B";

    // The third field is public const (hence also literal and static),
    // with a default value.
    public const string FieldC = "String C";
}

public class Myfieldattributes
{
    public static void Main()
    {
        Console.WriteLine ("\nReflection.FieldAttributes");
        Demo d = new Demo();
 
        // Get a Type object for Demo, and a FieldInfo for each of
        // the three fields. Use the FieldInfo to display field
        // name, value for the Demo object in d, and attributes.
        //
        Type myType = typeof(Demo);
        FieldInfo fiPrivate = myType.GetField("m_field",
            BindingFlags.NonPublic | BindingFlags.Instance);
        DisplayField(d, fiPrivate);

        FieldInfo fiPublic = myType.GetField("Field",
            BindingFlags.Public | BindingFlags.Instance);
        DisplayField(d, fiPublic);

        FieldInfo fiConstant = myType.GetField("FieldC",
            BindingFlags.Public | BindingFlags.Static);
        DisplayField(d, fiConstant);
    }

    static void DisplayField(Object obj, FieldInfo f)
    { 
        // Display the field name, value, and attributes.
        //
        Console.WriteLine("{0} = \"{1}\"; attributes: {2}", 
            f.Name, f.GetValue(obj), f.Attributes);
    }
}

/* This code example produces the following output:

Reflection.FieldAttributes
m_field = "String A"; attributes: Private
Field = "String B"; attributes: Public
FieldC = "String C"; attributes: Public, Static, Literal, HasDefault
 */
using namespace System;
using namespace System::Reflection;
using namespace System::Security::Permissions;

public ref class Demo
{
private:
    // Make three fields:
    // The first field is private.
    String^ m_field;

    // The second field is public.
public:
    String^ Field;

    // The third field is public and literal. 
    literal String^ FieldC = "String C";

    Demo() { m_field = "String A"; Field = "String B"; }
};

static void DisplayField(Object^ obj, FieldInfo^ f)
{ 
    // Display the field name, value, and attributes.
    //
    Console::WriteLine("{0} = \"{1}\"; attributes: {2}", 
        f->Name, f->GetValue(obj), f->Attributes);
};

void main()
{
    Console::WriteLine ("\nReflection.FieldAttributes");
    Demo^ d = gcnew Demo();

    // Get a Type object for Demo, and a FieldInfo for each of
    // the three fields. Use the FieldInfo to display field
    // name, value for the Demo object in d, and attributes.
    //
    Type^ myType = Demo::typeid;

    FieldInfo^ fiPrivate = myType->GetField("m_field",
        BindingFlags::NonPublic | BindingFlags::Instance);
    DisplayField(d, fiPrivate);

    FieldInfo^ fiPublic = myType->GetField("Field",
        BindingFlags::Public | BindingFlags::Instance);
    DisplayField(d, fiPublic);

    FieldInfo^ fiConstant = myType->GetField("FieldC",
        BindingFlags::Public | BindingFlags::Static);
    DisplayField(d, fiConstant);
}

/* This code example produces the following output:

Reflection.FieldAttributes
m_field = "String A"; attributes: Private
Field = "String B"; attributes: Public
FieldC = "String C"; attributes: Public, Static, Literal, HasDefault
 */

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

System.Reflection 命名空间