EN
Ce contenu n’est pas disponible dans votre langue. Voici la version anglaise.

Type.GetProperties Method

Gets the properties of the current Type.

Overload List

Returns all the public properties of the current Type.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function GetProperties() As PropertyInfo()
[C#] public PropertyInfo[] GetProperties();
[C++] public: PropertyInfo* GetProperties() [];
[JScript] public function GetProperties() : PropertyInfo[];

When overridden in a derived class, searches for the properties of the current Type, using the specified binding constraints.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public MustOverride Function GetProperties(BindingFlags) As PropertyInfo() Implements IReflect.GetProperties
[C#] public abstract PropertyInfo[] GetProperties(BindingFlags);
[C++] public: virtual PropertyInfo* GetProperties(BindingFlags) [] = 0;
[JScript] public abstract function GetProperties(BindingFlags) : PropertyInfo[];

Example

[Visual Basic, C#, C++] The following example creates two public properties and one protected property and displays information for the properties that match the specified binding constraints.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of GetProperties. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.Reflection
Imports System.Reflection.Emit
Imports Microsoft.VisualBasic

' Create a class having three properties.
Public Class MyTypeClass
    Public ReadOnly Property MyProperty1() As [String]
        Get
            Return "hello"
        End Get
    End Property
    Public ReadOnly Property MyProperty2() As [String]
        Get
            Return "hello"
        End Get
    End Property
    Protected ReadOnly Property MyProperty3() As [String]
        Get
            Return "hello"
        End Get
    End Property
End Class 'MyTypeClass

Public Class TypeMain
    Public Shared Sub Main()
        Dim myType As Type = GetType(MyTypeClass)
        ' Get the public properties.
        Dim myPropertyInfo As PropertyInfo() = myType.GetProperties((BindingFlags.Public Or BindingFlags.Instance))
        Console.WriteLine("The number of public properties is {0}.", myPropertyInfo.Length.ToString())
        ' Display the public properties.
        DisplayPropertyInfo(myPropertyInfo)
        ' Get the nonpublic properties.
        Dim myPropertyInfo1 As PropertyInfo() = myType.GetProperties((BindingFlags.NonPublic Or BindingFlags.Instance))
        Console.WriteLine("The number of protected properties is {0}.", myPropertyInfo1.Length.ToString())
        ' Display the nonpublic properties.
        DisplayPropertyInfo(myPropertyInfo1)
    End Sub 'Main

    Public Shared Sub DisplayPropertyInfo(ByVal myPropertyInfo() As PropertyInfo)
        ' Display the information for all properties.
        Dim i As Integer
        For i = 0 To myPropertyInfo.Length - 1
            Dim myPropInfo As PropertyInfo = CType(myPropertyInfo(i), PropertyInfo)
            Console.WriteLine("The property name is {0}.", myPropInfo.Name.ToString())
            Console.WriteLine("The property type is {0}.", myPropInfo.PropertyType.ToString())
        Next i
    End Sub 'DisplayPropertyInfo
End Class 'TypeMain 

[C#] 

using System;
using System.Reflection;
using System.Reflection.Emit;

// Create a class having three properties.
public class MyTypeClass
{
    public String MyProperty1
    {
        get 
        {
            return "hello";
        }
    }
    public String MyProperty2 
    {
        get 
        {
            return "hello";
        }
    }
    protected String MyProperty3
    {
        get
        {
            return "hello";
        }
    }
}

public class TypeMain
{
    public static void Main() 
    {
        Type myType =(typeof(MyTypeClass));
        // Get the public properties.
        PropertyInfo[] myPropertyInfo = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
        Console.WriteLine("The mumber of public properties is {0}.", myPropertyInfo.Length);
        // Display the public properties.
        DisplayPropertyInfo(myPropertyInfo);
        // Get the nonpublic properties.
        PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.NonPublic|BindingFlags.Instance);
        Console.WriteLine("The number of protected properties is {0}.", myPropertyInfo1.Length);
        // Display all the nonpublic properties.
        DisplayPropertyInfo(myPropertyInfo1);        
    }
    public static void DisplayPropertyInfo(PropertyInfo[] myPropertyInfo)
    {
        // Display information for all properties.
        for(int i=0;i<myPropertyInfo.Length;i++)
        {
            PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo[i];
            Console.WriteLine("The property name is {0}.", myPropInfo.Name);
            Console.WriteLine("The property type is {0}.", myPropInfo.PropertyType);
        }
    }
}

[C++] 

#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

// Create a class having three properties.
public __gc class MyTypeClass {
public:
   __property String* get_MyProperty1() {
      return S"hello";
   }
   __property String* get_MyProperty2() {
      return S"hello";
   }
protected:
   __property String* get_MyProperty3() {
      return S"hello";
   }
};

void DisplayPropertyInfo(PropertyInfo* myPropertyInfo[]) {
   // Display information for all properties.
   for (int i=0;i<myPropertyInfo->Length;i++) {
      PropertyInfo*  myPropInfo = myPropertyInfo[i];
      Console::WriteLine(S"The property name is {0}.", myPropInfo->Name);
      Console::WriteLine(S"The property type is {0}.", myPropInfo->PropertyType);
   }
}
int main() {
   Type* myType =(__typeof(MyTypeClass));
   // Get the public properties.
   PropertyInfo*  myPropertyInfo[] = myType->GetProperties(static_cast<BindingFlags>(BindingFlags::Public|BindingFlags::Instance));
   Console::WriteLine(S"The mumber of public properties is {0}.",__box(  myPropertyInfo->Length));
   // Display the public properties.
   DisplayPropertyInfo(myPropertyInfo);
   // Get the nonpublic properties.
   PropertyInfo*  myPropertyInfo1[] = myType->GetProperties(static_cast<BindingFlags>(BindingFlags::NonPublic|BindingFlags::Instance));
   Console::WriteLine(S"The number of protected properties is {0}.",__box( myPropertyInfo1->Length));
   // Display all the nonpublic properties.
   DisplayPropertyInfo(myPropertyInfo1);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Type Class | Type Members | System Namespace

Afficher:
© 2014 Microsoft