FieldInfo 类

定义

发现字段的属性并提供对字段元数据的访问权限。Discovers the attributes of a field and provides access to field metadata.

public ref class FieldInfo abstract : System::Reflection::MemberInfo, System::Runtime::InteropServices::_FieldInfo
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public abstract class FieldInfo : System.Reflection.MemberInfo, System.Runtime.InteropServices._FieldInfo
type FieldInfo = class
    inherit MemberInfo
    interface _FieldInfo
Public MustInherit Class FieldInfo
Inherits MemberInfo
Implements _FieldInfo
继承
FieldInfo
派生
属性
实现

示例

下面的示例使用Type.GetFields方法获取FieldInfo类中与字段相关的信息,然后显示字段属性。The following example uses the Type.GetFields method to get the field-related information from the FieldInfo class, and then displays field attributes.

using namespace System;
using namespace System::Reflection;
public ref class FieldInfoClass
{
public:
   int myField1;

protected:
   String^ myField2;
};

int main()
{
   array<FieldInfo^>^myFieldInfo;
   Type^ myType = FieldInfoClass::typeid;

   // Get the type and fields of FieldInfoClass.
   myFieldInfo = myType->GetFields( static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance | BindingFlags::Public) );
   Console::WriteLine( "\nThe fields of FieldInfoClass are \n" );

   // Display the field information of FieldInfoClass.
   for ( int i = 0; i < myFieldInfo->Length; i++ )
   {
      Console::WriteLine( "\nName            : {0}", myFieldInfo[ i ]->Name );
      Console::WriteLine( "Declaring Type  : {0}", myFieldInfo[ i ]->DeclaringType );
      Console::WriteLine( "IsPublic        : {0}", myFieldInfo[ i ]->IsPublic );
      Console::WriteLine( "MemberType      : {0}", myFieldInfo[ i ]->MemberType );
      Console::WriteLine( "FieldType       : {0}", myFieldInfo[ i ]->FieldType );
      Console::WriteLine( "IsFamily        : {0}", myFieldInfo[ i ]->IsFamily );
   }
}
using System;
using System.Reflection;

public class FieldInfoClass
{
    public int myField1 = 0;
    protected string myField2 = null;
    public static void Main()
    {
        FieldInfo[] myFieldInfo;
        Type myType = typeof(FieldInfoClass);
        // Get the type and fields of FieldInfoClass.
        myFieldInfo = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance
            | BindingFlags.Public);
        Console.WriteLine("\nThe fields of " + 
            "FieldInfoClass are \n");
        // Display the field information of FieldInfoClass.
        for(int i = 0; i < myFieldInfo.Length; i++)
        {
            Console.WriteLine("\nName            : {0}", myFieldInfo[i].Name);
            Console.WriteLine("Declaring Type  : {0}", myFieldInfo[i].DeclaringType);
            Console.WriteLine("IsPublic        : {0}", myFieldInfo[i].IsPublic);
            Console.WriteLine("MemberType      : {0}", myFieldInfo[i].MemberType);
            Console.WriteLine("FieldType       : {0}", myFieldInfo[i].FieldType);
            Console.WriteLine("IsFamily        : {0}", myFieldInfo[i].IsFamily);
        }
    }
}
Imports System.Reflection

Public Class FieldInfoClass
    Public myField1 As Integer = 0
    Protected myField2 As String = Nothing

    Public Shared Sub Main()
        Dim myFieldInfo() As FieldInfo
        Dim myType As Type = GetType(FieldInfoClass)
        ' Get the type and fields of FieldInfoClass.
        myFieldInfo = myType.GetFields(BindingFlags.NonPublic Or _
                      BindingFlags.Instance Or BindingFlags.Public)
        Console.WriteLine(ControlChars.NewLine & "The fields of " & _
                      "FieldInfoClass class are " & ControlChars.NewLine)
        ' Display the field information of FieldInfoClass.
        Dim i As Integer
        For i = 0 To myFieldInfo.Length - 1
            Console.WriteLine(ControlChars.NewLine + "Name            : {0}", myFieldInfo(i).Name)
            Console.WriteLine("Declaring Type  : {0}", myFieldInfo(i).DeclaringType)
            Console.WriteLine("IsPublic        : {0}", myFieldInfo(i).IsPublic)
            Console.WriteLine("MemberType      : {0}", myFieldInfo(i).MemberType)
            Console.WriteLine("FieldType       : {0}", myFieldInfo(i).FieldType)
            Console.WriteLine("IsFamily        : {0}", myFieldInfo(i).IsFamily)
        Next i
    End Sub
End Class

注解

字段信息是从元数据获取的。The field information is obtained from metadata. FieldInfo类没有公共构造函数。The FieldInfo class does not have a public constructor. FieldInfo对象是通过调用GetFields Type对象的或GetField方法获取的。FieldInfo objects are obtained by calling either the GetFields or GetField method of a Type object.

字段是在类中定义的变量。Fields are variables defined in the class. FieldInfo提供对类中字段的元数据的访问,并为字段提供动态集和获取功能。FieldInfo provides access to the metadata for a field within a class and provides dynamic set and get functionality for the field. 类不会加载到内存中,直到对对象调用 invoke 或 get。The class is not loaded into memory until invoke or get is called on the object.

实施者说明

当从 FieldInfo 继承时,必须重写下面的成员:GetValue(Object)SetValue(Object, Object, BindingFlags, Binder, CultureInfo)When you inherit from FieldInfo, you must override the following members: GetValue(Object) and SetValue(Object, Object, BindingFlags, Binder, CultureInfo).

构造函数

FieldInfo()

初始化 FieldInfo 类的新实例。Initializes a new instance of the FieldInfo class.

属性

Attributes

获取与此字段关联的特性。Gets the attributes associated with this field.

CustomAttributes

获取包含此成员自定义属性的集合。Gets a collection that contains this member's custom attributes.

(继承自 MemberInfo)
DeclaringType

获取声明该成员的类。Gets the class that declares this member.

(继承自 MemberInfo)
FieldHandle

获取 RuntimeFieldHandle,它是字段的内部元数据表示形式的句柄。Gets a RuntimeFieldHandle, which is a handle to the internal metadata representation of a field.

FieldType

获取此字段对象的类型。Gets the type of this field object.

IsAssembly

获取一个值,该值指示此字段的潜在可见性是否由 Assembly 描述;也就是说,此字段只对同一程序集中的其他类型可见,而对该程序集以外的派生类型则不可见。Gets a value indicating whether the potential visibility of this field is described by Assembly; that is, the field is visible at most to other types in the same assembly, and is not visible to derived types outside the assembly.

IsCollectible

获取一个值,该值指示此 MemberInfo 对象是否是包含在可回收的 AssemblyLoadContext 中的程序集的一部分。Gets a value that indicates whether this MemberInfo object is part of an assembly held in a collectible AssemblyLoadContext.

(继承自 MemberInfo)
IsFamily

获取一个值,该值指示此字段的可见性是否由 Family 描述;也就是说,此字段仅在其类和派生类内可见。Gets a value indicating whether the visibility of this field is described by Family; that is, the field is visible only within its class and derived classes.

IsFamilyAndAssembly

获取一个值,该值指示此字段的可见性是否由 FamANDAssem 描述;也就是说,可从派生类访问此字段,但仅当这些派生类在同一程序集中时。Gets a value indicating whether the visibility of this field is described by FamANDAssem; that is, the field can be accessed from derived classes, but only if they are in the same assembly.

IsFamilyOrAssembly

获取一个值,该值指示此字段的潜在可见性是否由 FamORAssem 描述;也就是说,可通过派生类(无论其位置如何)和同一程序集中的类访问此字段。Gets a value indicating whether the potential visibility of this field is described by FamORAssem; that is, the field can be accessed by derived classes wherever they are, and by classes in the same assembly.

IsInitOnly

获取一个值,通过该值指示此字段是否只能在构造函数的主体中设置。Gets a value indicating whether the field can only be set in the body of the constructor.

IsLiteral

获取一个值,通过该值指示该值是否在编译时写入并且不能更改。Gets a value indicating whether the value is written at compile time and cannot be changed.

IsNotSerialized

获取一个值,通过该值指示此字段是否有 NotSerialized 特性。Gets a value indicating whether this field has the NotSerialized attribute.

IsPinvokeImpl

获取一个值,该值指示是否已在 FieldAttributes 中设置相应的 PinvokeImpl 特性。Gets a value indicating whether the corresponding PinvokeImpl attribute is set in FieldAttributes.

IsPrivate

获取一个值,通过该值指示此字段是否为私有字段。Gets a value indicating whether the field is private.

IsPublic

获取一个值,通过该值指示此字段是否为公共字段。Gets a value indicating whether the field is public.

IsSecurityCritical

获取一个值,该值指示当前字段在当前信任级别上是安全关键的还是安全可靠关键的。Gets a value that indicates whether the current field is security-critical or security-safe-critical at the current trust level.

IsSecuritySafeCritical

获取一个值,该值指示当前字段在当前信任级别上是否是安全可靠关键的。Gets a value that indicates whether the current field is security-safe-critical at the current trust level.

IsSecurityTransparent

获取一个值,该值指示当前字段在当前信任级别上是否是透明的。Gets a value that indicates whether the current field is transparent at the current trust level.

IsSpecialName

获取一个值,该值指示是否已在 FieldAttributes 枚举数中设置相应的 SpecialName 特性。Gets a value indicating whether the corresponding SpecialName attribute is set in the FieldAttributes enumerator.

IsStatic

获取一个值,通过该值指示此字段是否为静态字段。Gets a value indicating whether the field is static.

MemberType

获取 MemberTypes 值,该值指示此成员是字段。Gets a MemberTypes value indicating that this member is a field.

MetadataToken

获取一个值,该值标识元数据元素。Gets a value that identifies a metadata element.

(继承自 MemberInfo)
Module

获取一个模块,在该模块中已经定义一个类型,该类型用于声明由当前 MemberInfo 表示的成员。Gets the module in which the type that declares the member represented by the current MemberInfo is defined.

(继承自 MemberInfo)
Name

获取当前成员的名称。Gets the name of the current member.

(继承自 MemberInfo)
ReflectedType

获取用于获取 MemberInfo 的此实例的类对象。Gets the class object that was used to obtain this instance of MemberInfo.

(继承自 MemberInfo)

方法

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

GetCustomAttributes(Boolean)

在派生类中重写时,返回应用于此成员的所有自定义属性的数组。When overridden in a derived class, returns an array of all custom attributes applied to this member.

(继承自 MemberInfo)
GetCustomAttributes(Type, Boolean)

在派生类中重写时,返回应用于此成员并由 Type 标识的自定义属性的数组。When overridden in a derived class, returns an array of custom attributes applied to this member and identified by Type.

(继承自 MemberInfo)
GetCustomAttributesData()

返回 CustomAttributeData 对象列表,这些对象表示已应用到目标成员的特性相关数据。Returns a list of CustomAttributeData objects representing data about the attributes that have been applied to the target member.

(继承自 MemberInfo)
GetFieldFromHandle(RuntimeFieldHandle)

获取由指定句柄表示的字段的 FieldInfoGets a FieldInfo for the field represented by the specified handle.

GetFieldFromHandle(RuntimeFieldHandle, RuntimeTypeHandle)

获取由指定句柄表示的指定泛型类型字段的 FieldInfoGets a FieldInfo for the field represented by the specified handle, for the specified generic type.

GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

GetOptionalCustomModifiers()

获取一个类型数组,这些类型标识字段的可选自定义修饰符。Gets an array of types that identify the optional custom modifiers of the field.

GetRawConstantValue()

由编译器返回与字段关联的文本值。Returns a literal value associated with the field by a compiler.

GetRequiredCustomModifiers()

获取一个类型数组,这些类型标识属性所需的自定义修饰符。Gets an array of types that identify the required custom modifiers of the property.

GetType()
GetValue(Object)

当在派生类中重写时,返回给定对象支持的字段的值。When overridden in a derived class, returns the value of a field supported by a given object.

GetValueDirect(TypedReference)

返回给定对象所支持的字段的值。Returns the value of a field supported by a given object.

HasSameMetadataDefinitionAs(MemberInfo) (继承自 MemberInfo)
IsDefined(Type, Boolean)

在派生类中重写时,指示是否将指定类型或其派生类型的一个或多个特性应用于此成员。When overridden in a derived class, indicates whether one or more attributes of the specified type or of its derived types is applied to this member.

(继承自 MemberInfo)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
SetValue(Object, Object)

设置给定对象支持的字段的值。Sets the value of the field supported by the given object.

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

在派生类中被重写时,设置给定对象支持的字段的值。When overridden in a derived class, sets the value of the field supported by the given object.

SetValueDirect(TypedReference, Object)

设置给定对象支持的字段的值。Sets the value of the field supported by the given object.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

操作员

Equality(FieldInfo, FieldInfo)

指示两个 FieldInfo 对象是否相等。Indicates whether two FieldInfo objects are equal.

Inequality(FieldInfo, FieldInfo)

指示两个 FieldInfo 对象是否不相等。Indicates whether two FieldInfo objects are not equal.

显式界面实现

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

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

_FieldInfo.GetType()

获取表示 Type 类型的 FieldInfo 对象。Gets a Type object representing the FieldInfo type.

_FieldInfo.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can then be used to get the type information for an interface.

_FieldInfo.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

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

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

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

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(继承自 MemberInfo)
_MemberInfo.GetType()

获取一个表示 MemberInfo 类的 Type 对象。Gets a Type object representing the MemberInfo class.

(继承自 MemberInfo)
_MemberInfo.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can then be used to get the type information for an interface.

(继承自 MemberInfo)
_MemberInfo.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(继承自 MemberInfo)
_MemberInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(继承自 MemberInfo)
ICustomAttributeProvider.GetCustomAttributes(Boolean) (继承自 MemberInfo)
ICustomAttributeProvider.GetCustomAttributes(Type, Boolean) (继承自 MemberInfo)
ICustomAttributeProvider.IsDefined(Type, Boolean) (继承自 MemberInfo)

扩展方法

GetCustomAttribute(MemberInfo, Type)

检索应用于指定成员的指定类型的自定义特性。Retrieves a custom attribute of a specified type that is applied to a specified member.

GetCustomAttribute(MemberInfo, Type, Boolean)

检索应用于指定成员的指定类型的自定义特性,并可选择检查该成员的上级。Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member.

GetCustomAttribute<T>(MemberInfo)

检索应用于指定成员的指定类型的自定义特性。Retrieves a custom attribute of a specified type that is applied to a specified member.

GetCustomAttribute<T>(MemberInfo, Boolean)

检索应用于指定成员的指定类型的自定义特性,并可选择检查该成员的上级。Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member.

GetCustomAttributes(MemberInfo)

检索应用于指定成员的自定义特性集合。Retrieves a collection of custom attributes that are applied to a specified member.

GetCustomAttributes(MemberInfo, Boolean)

检索应用于指定成员的自定义特性集合,并可选择检查该成员的上级。Retrieves a collection of custom attributes that are applied to a specified member, and optionally inspects the ancestors of that member.

GetCustomAttributes(MemberInfo, Type)

检索应用于指定成员的指定类型的自定义特性集合。Retrieves a collection of custom attributes of a specified type that are applied to a specified member.

GetCustomAttributes(MemberInfo, Type, Boolean)

检索应用于指定成员的指定类型的自定义特性集合,并可选择检查该成员的上级。Retrieves a collection of custom attributes of a specified type that are applied to a specified member, and optionally inspects the ancestors of that member.

GetCustomAttributes<T>(MemberInfo)

检索应用于指定成员的指定类型的自定义特性集合。Retrieves a collection of custom attributes of a specified type that are applied to a specified member.

GetCustomAttributes<T>(MemberInfo, Boolean)

检索应用于指定成员的指定类型的自定义特性集合,并可选择检查该成员的上级。Retrieves a collection of custom attributes of a specified type that are applied to a specified member, and optionally inspects the ancestors of that member.

IsDefined(MemberInfo, Type)

确定是否将指定类型的任何自定义属性应用于指定的成员。Indicates whether custom attributes of a specified type are applied to a specified member.

IsDefined(MemberInfo, Type, Boolean)

指示一个指定类型的自定义特性是否应用于一个指定的数字,并选择性地应用于其的上级。Indicates whether custom attributes of a specified type are applied to a specified member, and, optionally, applied to its ancestors.

GetMetadataToken(MemberInfo)

获取给定成员的元数据令牌(如果可用)。Gets a metadata token for the given member, if available.

HasMetadataToken(MemberInfo)

返回表示元数据令牌是否可用于指定的成员的值。Returns a value that indicates whether a metadata token is available for the specified member.

安全性

InheritanceDemand
完全信任继承者。for full trust for inheritors. 此类不能由部分信任的代码继承。This class cannot be inherited by partially trusted code.

适用于

线程安全性

此类型是线程安全的。This type is thread safe.

另请参阅