Type.GetProperties Method (BindingFlags)

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public MustOverride Function GetProperties ( _
	bindingAttr As BindingFlags _
) As PropertyInfo()

Parameters

bindingAttr
Type: System.Reflection.BindingFlags

A bitmask comprised of one or more BindingFlags that specify how the search is conducted.

-or-

Zero, to return Nothing.

Return Value

Type: System.Reflection.PropertyInfo()
An array of PropertyInfo objects representing all properties of the current Type that match the specified binding constraints.
-or-
An empty array of type PropertyInfo, if the current Type does not have properties, or if none of the properties match the binding constraints.

Implements

_Type.GetProperties(BindingFlags)
IReflect.GetProperties(BindingFlags)

A property is considered public to reflection if it has at least one accessor that is public. Otherwise the property is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

The GetProperties method does not return properties in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which properties are returned, because that order varies.

The following BindingFlags filter flags can be used to define which nested types to include in the search:

  • You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.

  • Specify BindingFlags.Public to include public properties in the search.

  • Specify BindingFlags.NonPublic to include non-public properties (that is, private, internal, and protected properties) in the search. Only protected and internal properties on base classes are returned; private properties on base classes are not returned.

  • Specify BindingFlags.FlattenHierarchy to include public and protected static members up the hierarchy; private static members in inherited classes are not included.

The following BindingFlags modifier flags can be used to change how the search works:

  • BindingFlags.DeclaredOnly to search only the properties declared on the Type, not properties that were simply inherited.

See System.Reflection.BindingFlags for more information.

A property is considered public to reflection if it has at least one accessor that is public. Otherwise the property is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

If the current T:System.Type represents a constructed generic type, this method returns the PropertyInfo 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 properties of the class constraint.

The following example defines a class named PropertyClass that includes six properties: two are public, one is private, one is protected, one is internal (Friend in Visual Basic), and one is protected internal (Protected Friend in Visual Basic). It then displays some basic property information (the property name and type, whether it is read/write, and the visibility of its get and set accessors) for the properties that match the specified binding constraints.

Imports System.Reflection

' Create a class having six properties. 
Public Class PropertyClass
    Public ReadOnly Property Property1() As String 
        Get 
            Return "hello" 
        End Get 
    End Property 

    Public ReadOnly Property Property2() As String 
        Get 
            Return "hello" 
        End Get 
    End Property 

    Protected ReadOnly Property Property3() As String 
        Get 
            Return "hello" 
        End Get 
    End Property 

    Private ReadOnly Property Property4 As Integer 
        Get 
           Return 32
        End Get 
    End Property 

    Friend ReadOnly Property Property5 As String 
       Get 
          Return "value" 
       End Get 
    End Property 

    Protected Friend ReadOnly Property Property6 As String 
       Get 
          Return "value" 
       End Get 
    End Property 
End Class 

Public Module Example
    Public Sub Main()
        Dim t As Type = GetType(PropertyClass)
        ' Get the public properties. 
        Dim propInfos As PropertyInfo() = t.GetProperties(BindingFlags.Public Or BindingFlags.Instance)
        Console.WriteLine("The number of public properties: {0}",
                          propInfos.Length)
        Console.WriteLine()
        ' Display the public properties.
        DisplayPropertyInfo(propInfos)

        ' Get the non-public properties. 
        Dim propInfos1 As PropertyInfo() = t.GetProperties(BindingFlags.NonPublic Or BindingFlags.Instance)
        Console.WriteLine("The number of non-public properties: {0}",
                          propInfos1.Length)
        Console.WriteLine()
        ' Display all the non-public properties.
        DisplayPropertyInfo(propInfos1)
    End Sub 

    Public Sub DisplayPropertyInfo(ByVal propInfos() As PropertyInfo)
        ' Display information for all properties. 
        For Each propInfo In propInfos
            Dim readable As Boolean = propInfo.CanRead
            Dim writable As Boolean = propInfo.CanWrite

            Console.WriteLine("   Property name: {0}", propInfo.Name)
            Console.WriteLine("   Property type: {0}", propInfo.PropertyType)
            Console.WriteLine("   Read-Write:    {0}", readable And writable)
            If readable Then 
               Dim getAccessor As MethodInfo = propInfo.GetMethod
               Console.WriteLine("   Visibility:    {0}",
                                 GetVisibility(getAccessor))
            End If 
            If writable Then 
               Dim setAccessor As MethodInfo = propInfo.SetMethod
               Console.WriteLine("   Visibility:    {0}",
                                 GetVisibility(setAccessor))
            End If
            Console.WriteLine()
        Next 
    End Sub 

    Public Function GetVisibility(accessor As MethodInfo) As String 
       If accessor.IsPublic Then 
          Return "Public" 
       ElseIf accessor.IsPrivate Then 
          Return "Private" 
       Else If accessor.IsFamily Then 
          Return "Protected" 
       Else If accessor.IsAssembly Then 
          Return "Internal/Friend" 
       Else 
          Return "Protected Internal/Friend" 
       End If 
    End Function 
End Module 
' The example displays the following output: 
'       The number of public properties: 2 

'          Property name: Property1 
'          Property type: System.String 
'          Read-Write:    False 
'          Visibility:    Public 

'          Property name: Property2 
'          Property type: System.String 
'          Read-Write:    False 
'          Visibility:    Public 

'       The number of non-public properties: 4 

'          Property name: Property3 
'          Property type: System.String 
'          Read-Write:    False 
'          Visibility:    Protected 

'          Property name: Property4 
'          Property type: System.Int32 
'          Read-Write:    False 
'          Visibility:    Private 

'          Property name: Property5 
'          Property type: System.String 
'          Read-Write:    False 
'          Visibility:    Internal/Friend 

'          Property name: Property6 
'          Property type: System.String 
'          Read-Write:    False 
'          Visibility:    Protected Internal/Friend

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft