Type.GetFields Method ()
Assembly: mscorlib (in mscorlib.dll)
public final FieldInfo[] GetFields ()
public final function GetFields () : FieldInfo[]
Not applicable.
Return Value
An array of FieldInfo objects representing all the public fields defined for the current Type. -or- An empty array of type FieldInfo, if no public fields are defined for the current Type.The GetFields method does not return fields in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which fields are returned, because that order varies.
The following table shows what members of a base class are returned by the Get methods when reflecting on a type.
| Member Type | Static | Non-Static |
|---|---|---|
| Constructor | No | No |
| Field | No | Yes. A field is always hide-by-name-and-signature. |
| Event | Not applicable | The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below. |
| Method | No | Yes. A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature. |
| Nested Type | No | No |
| Property | Not applicable | The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below. |
-
Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. This is a binary comparison.
-
For reflection, properties and events are hide-by-name-and-signature. If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.
-
Custom attributes are not part of the common type system.
If the current Type represents a constructed generic type, this method returns the FieldInfo objects with the type parameters replaced by the appropriate type arguments.
If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the public fields of the class constraint.
The following example shows a use of the GetFields() method.
#using <system.dll> using namespace System; using namespace System::Reflection; using namespace System::ComponentModel::Design; int main() { try { // Get the type handle of a specified class. Type^ myType = ViewTechnology::typeid; // Get the fields of the specified class. array<FieldInfo^>^myField = myType->GetFields(); Console::WriteLine( "\nDisplaying fields that have SpecialName attributes:\n" ); for ( int i = 0; i < myField->Length; i++ ) { // Determine whether or not each field is a special name. if ( myField[ i ]->IsSpecialName ) { Console::WriteLine( "The field {0} has a SpecialName attribute.", myField[ i ]->Name ); } } } catch ( Exception^ e ) { Console::WriteLine( "Exception : {0} ", e->Message ); } }
import System.*;
import System.Reflection.*;
import System.ComponentModel.Design.*;
class FieldInfoIsSpecialName
{
public static void main(String[] args)
{
try {
// Get the type handle of a specified class.
Type myType = ViewTechnology.class.ToType();
// Get the fields of the specified class.
FieldInfo myField[] = myType.GetFields();
Console.WriteLine("\nDisplaying fields that have SpecialName"
+ " attributes:\n");
for (int i = 0; i < myField.length; i++) {
// Determine whether or not each field is a special name.
if (myField[i].get_IsSpecialName()) {
Console.WriteLine("The field {0} has a SpecialName"
+ " attribute.", myField[i].get_Name());
}
}
}
catch (System.Exception e) {
Console.WriteLine("Exception : {0} ", e.get_Message());
}
} //main
} //FieldInfoIsSpecialName
Windows 98, Windows Server 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
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.