Assembly : mscorlib (dans mscorlib.dll)
SyntaxePublic MustOverride Function GetFields ( _ bindingAttr As BindingFlags _ ) As FieldInfo()
Dim instance As Type Dim bindingAttr As BindingFlags Dim returnValue As FieldInfo() returnValue = instance.GetFields(bindingAttr)
public abstract FieldInfo[] GetFields (
BindingFlags bindingAttr
)
public:
virtual array<FieldInfo^>^ GetFields (
BindingFlags bindingAttr
) abstract
public abstract FieldInfo[] GetFields (
BindingFlags bindingAttr
)
public abstract function GetFields ( bindingAttr : BindingFlags ) : FieldInfo[]
Paramètres
- bindingAttr
Masque de bits constitué d'un ou de plusieurs BindingFlags spécifiant le mode d'exécution de la recherche.
? ou ?
Zéro, pour retourner référence Null (Nothing en Visual Basic).
Valeur de retour
Tableau d'objets FieldInfo représentant tous les champs définis pour le Type en cours qui correspondent aux contraintes de liaison spécifiées. ? ou ? Tableau vide du type FieldInfo si aucun champ n'est défini pour le Type en cours ou si aucun des champs définis ne correspond aux contraintes de liaison.
NotesLa méthode GetFields ne retourne pas les champs dans un ordre particulier, tel que l'ordre alphabétique ou l'ordre de déclaration. Votre code ne doit pas dépendre de l'ordre dans lequel les champs sont retournés, car cet ordre peut varier.
Les indicateurs de filtre BindingFlags suivants peuvent être utilisés pour définir les champs à inclure dans la recherche :
-
Vous devez spécifier soit BindingFlags.Instance, soit BindingFlags.Static pour obtenir un retour.
-
Spécifiez BindingFlags.Public pour inclure les champs publics dans la recherche.
-
Spécifiez BindingFlags.NonPublic pour inclure les champs non publics (c'est-à-dire les champs privés et protégés) dans la recherche.
-
Spécifiez BindingFlags.FlattenHierarchy pour inclure les membres statiques public et protected en haut de la hiérarchie ; les membres statiques private dans les classes héritées ne sont pas inclus.
Les indicateurs de modificateur BindingFlags suivants peuvent être utilisés pour modifier le fonctionnement de la recherche :
-
BindingFlags.DeclaredOnly pour limiter la recherche aux champs déclarés dans Type et exclure ceux qui ont été simplement hérités.
Pour plus d'informations, consultez System.Reflection.BindingFlags.
Si le type demandé est non public et si l'appelant ne possède pas de ReflectionPermission pour réfléchir les objets non publics en dehors de l'assembly en cours, cette méthode retourne alors référence Null (Nothing en Visual Basic).
Si le Type en cours représente un type générique construit, cette méthode retourne les objets FieldInfo avec les paramètres de type remplacés par les arguments de type appropriés.
Si le Type en cours représente un paramètre de type dans la définition d'un type ou d'une méthode générique, cette méthode recherche les champs publics de la contrainte de classe.
ExempleL'exemple suivant illustre l'utilisation de la méthode GetFields(BindingFlags).
Imports System Imports System.Reflection Imports Microsoft.VisualBasic Class AttributesSample Public Sub Mymethod(ByVal int1m As Integer, ByRef str2m As String, ByRef str3m As String) str2m = "in Mymethod" End Sub 'Mymethod Public Shared Function Main(ByVal args() As String) As Integer Console.WriteLine("Reflection.MethodBase.Attributes Sample") ' Get the type. Dim MyType As Type = Type.GetType("AttributesSample") ' Get the method Mymethod on the type. Dim Mymethodbase As MethodBase = MyType.GetMethod("Mymethod") ' Display the method name. Console.WriteLine("Mymethodbase = {0}.", Mymethodbase) ' Get the MethodAttribute enumerated value. Dim Myattributes As MethodAttributes = Mymethodbase.Attributes ' Display the flags that are set. PrintAttributes(GetType(System.Reflection.MethodAttributes), CInt(Myattributes)) Return 0 End Function 'Main Public Shared Sub PrintAttributes(ByVal attribType As Type, ByVal iAttribValue As Integer) If Not attribType.IsEnum Then Console.WriteLine("This type is not an enum.") Return End If Dim fields As FieldInfo() = attribType.GetFields((BindingFlags.Public Or BindingFlags.Static)) Dim i As Integer For i = 0 To fields.Length - 1 Dim fieldvalue As Integer = CType(fields(i).GetValue(Nothing), Int32) If (fieldvalue And iAttribValue) = fieldvalue Then Console.WriteLine(fields(i).Name) End If Next i End Sub 'PrintAttributes End Class 'AttributesSample
using System; using System.Reflection; class AttributesSample { public void Mymethod (int int1m, out string str2m, ref string str3m) { str2m = "in Mymethod"; } public static int Main(string[] args) { Console.WriteLine ("Reflection.MethodBase.Attributes Sample"); // Get the type. Type MyType = Type.GetType("AttributesSample"); // Get the method Mymethod on the type. MethodBase Mymethodbase = MyType.GetMethod("Mymethod"); // Display the method name. Console.WriteLine("Mymethodbase = " + Mymethodbase); // Get the MethodAttribute enumerated value. MethodAttributes Myattributes = Mymethodbase.Attributes; // Display the flags that are set. PrintAttributes(typeof(System.Reflection.MethodAttributes), (int) Myattributes); return 0; } public static void PrintAttributes(Type attribType, int iAttribValue) { if (!attribType.IsEnum) { Console.WriteLine("This type is not an enum."); return; } FieldInfo[] fields = attribType.GetFields(BindingFlags.Public | BindingFlags.Static); for (int i = 0; i < fields.Length; i++) { int fieldvalue = (Int32)fields[i].GetValue(null); if ((fieldvalue & iAttribValue) == fieldvalue) { Console.WriteLine(fields[i].Name); } } } }
using namespace System; using namespace System::Reflection; using namespace System::Runtime::InteropServices; public ref class AttributesSample { public: void Mymethod( int int1m, [Out]interior_ptr<String^> str2m, interior_ptr<String^> str3m ) { *str2m = "in Mymethod"; } }; void PrintAttributes( Type^ attribType, int iAttribValue ) { if ( !attribType->IsEnum ) { Console::WriteLine( "This type is not an enum." ); return; } array<FieldInfo^>^fields = attribType->GetFields( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Static) ); for ( int i = 0; i < fields->Length; i++ ) { int fieldvalue = safe_cast<Int32>(fields[ i ]->GetValue( nullptr )); if ( (fieldvalue & iAttribValue) == fieldvalue ) { Console::WriteLine( fields[ i ]->Name ); } } } int main() { Console::WriteLine( "Reflection.MethodBase.Attributes Sample" ); // Get the type. Type^ MyType = Type::GetType( "AttributesSample" ); // Get the method Mymethod on the type. MethodBase^ Mymethodbase = MyType->GetMethod( "Mymethod" ); // Display the method name. Console::WriteLine( "Mymethodbase = {0}", Mymethodbase ); // Get the MethodAttribute enumerated value. MethodAttributes Myattributes = Mymethodbase->Attributes; // Display the flags that are set. PrintAttributes( System::Reflection::MethodAttributes::typeid, (int)Myattributes ); return 0; }
import System.*; import System.Reflection.*; class AttributesSample { public void MyMethod(int int1m, /** @ref */ String str2m, /** @ref */ String str3m) { str2m = "in MyMethod"; } //MyMethod public static void main(String[] args) { Console.WriteLine("Reflection.MethodBase.Attributes Sample"); // Get the type. Type myType = Type.GetType("AttributesSample"); // Get the method MyMethod on the type. MethodBase myMethodBase = myType.GetMethod("MyMethod"); // Display the method name. Console.WriteLine(("myMethodBase = " + myMethodBase)); // Get the MethodAttribute enumerated value. MethodAttributes myAttributes = myMethodBase.get_Attributes(); // Display the flags that are set. PrintAttributes(System.Reflection.MethodAttributes .class.ToType(), (int)(myAttributes)); } //main public static void PrintAttributes(Type attribType, int iAttribValue) { if (!(attribType.get_IsEnum())) { Console.WriteLine("This type is not an enum."); return ; } FieldInfo fields[] = attribType.GetFields( (BindingFlags.Public | BindingFlags.Static)); for(int i=0;i < fields.length;i++) { int fieldValue = (int) ((Int32)(fields[i].GetValue(null))); if ((fieldValue & iAttribValue) == fieldValue ) { Console.WriteLine(fields[i].get_Name()); } } } //PrintAttributes } //AttributesSample
import System; import System.Reflection; class AttributesSample { public function Mymethod (int1m : int) : void { } public static function Main() : void { Console.WriteLine ("Reflection.MethodBase.Attributes Sample"); // Get our type var MyType : Type = Type.GetType("AttributesSample"); // Get the method Mymethod on our type var Mymethodbase : MethodBase = MyType.GetMethod("Mymethod"); // Print out the method Console.WriteLine("Mymethodbase = " + Mymethodbase); // Get the MethodAttribute enumerated value var Myattributes : MethodAttributes = Mymethodbase.Attributes; // print out the flags set PrintAttributes( System.Reflection.MethodAttributes, int(Myattributes) ); } public static function PrintAttributes( attribType : Type, iAttribValue : int ) : void { if ( ! attribType.IsEnum ) { Console.WriteLine( "This type is not an enum" ); return; } var fields : FieldInfo[] = attribType.GetFields(BindingFlags.Public | BindingFlags.Static); for ( var i:int = 0; i < fields.Length; i++ ) { var fieldvalue : int = int(fields[i].GetValue(null)); if ( (fieldvalue & iAttribValue) == fieldvalue ) { Console.WriteLine( "\t" + fields[i].Name ); } } } } AttributesSample.Main(); /* This code produces the following output: Reflection.MethodBase.Attributes Sample Mymethodbase = Void Mymethod(Int32) PrivateScope FamANDAssem Family Public Virtual HideBySig VtableLayoutMask ReuseSlot NewSlot */
Sécurité .NET Framework- ReflectionPermission pour réfléchir les objets non publics. Énumération associée : ReflectionPermissionFlag.TypeInformation
Plates-formesWindows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
Informations de version
Outils (masquer)
S'enregistrer
Liste des Membres
Qui est en ligne?
FAQ