Type.GetFields Method ()
Returns all the public fields of the current Type.
[Visual Basic] Overloads Public Function GetFields() As FieldInfo() [C#] public FieldInfo[] GetFields(); [C++] public: FieldInfo* GetFields() []; [JScript] public function GetFields() : FieldInfo[];
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.
Remarks
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 can vary.
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.
Example
[Visual Basic, C#, C++] The following example shows a use of the GetFields() method.
[Visual Basic] Imports System Imports System.Reflection Imports System.ComponentModel.Design Imports Microsoft.VisualBasic Class FieldInfo_IsSpecialName Public Shared Sub Main() Try ' Get the type handle of a specified class. Dim myType As Type = GetType(ViewTechnology) ' Get the fields of a specified class. Dim myField As FieldInfo() = myType.GetFields() Console.WriteLine(ControlChars.Cr + "Displaying fields that have SpecialName attributes:" + ControlChars.Cr) Dim i As Integer For i = 0 To myField.Length - 1 ' Determine whether or not each field is a special name. If myField(i).IsSpecialName Then Console.WriteLine("The field {0} has a SpecialName attribute.", myField(i).Name) End If Next i Catch e As Exception Console.WriteLine("Exception : {0} ", e.Message.ToString()) End Try End Sub 'Main End Class 'FieldInfo_IsSpecialName [C#] using System; using System.Reflection; using System.ComponentModel.Design; class FieldInfo_IsSpecialName { public static void Main() { try { // Get the type handle of a specified class. Type myType = typeof(ViewTechnology); // 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].IsSpecialName) { Console.WriteLine("The field {0} has a SpecialName attribute.", myField[i].Name); } } } catch(Exception e) { Console.WriteLine("Exception : {0} " , e.Message); } } } [C++] #using <mscorlib.dll> #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 = __typeof(ViewTechnology); // Get the fields of the specified class. FieldInfo* myField[] = myType->GetFields(); Console::WriteLine(S"\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(S"The field {0} has a SpecialName attribute.", myField[i]->Name); } } } catch (Exception* e) { Console::WriteLine(S"Exception : {0} " , e->Message); } }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard
See Also
Type Class | Type Members | System Namespace | Type.GetFields Overload List | FieldInfo | GetField