```html System.Reflection.PropertyInfo Class | Microsoft Docs

System.Reflection.PropertyInfo Class

Overview

The PropertyInfo class discovers the attributes of a property and provides access to property metadata. It derives from MemberInfo and implements ICustomAttributeProvider.

Inheritance

System.Object
   ↳ System.Reflection.MemberInfo
        ↳ System.Reflection.PropertyInfo

Properties

NameTypeDescription
CanReadboolGets a value indicating whether the property can be read.
CanWriteboolGets a value indicating whether the property can be written to.
PropertyTypeTypeGets the type of the property.
GetMethodMethodInfoGets the MethodInfo object for the property's get accessor.
SetMethodMethodInfoGets the MethodInfo object for the property's set accessor.
AttributesPropertyAttributesGets the attributes for this property.
IsSpecialNameboolIndicates whether the property has a specialname attribute.
ReflectedTypeTypeGets the class that was used to obtain this PropertyInfo object.
DeclaringTypeTypeGets the class that declares this property.

Methods

NameSignatureDescription
GetValueobject GetValue(object obj, object[] index)Gets the value of the property for a given object.
SetValuevoid SetValue(object obj, object value, object[] index)Sets the value of the property for a given object.
GetGetMethodMethodInfo GetGetMethod(bool nonPublic)Returns the public or non‑public get accessor.
GetSetMethodMethodInfo GetSetMethod(bool nonPublic)Returns the public or non‑public set accessor.
GetCustomAttributesobject[] GetCustomAttributes(bool inherit)Returns an array of custom attributes applied to this property.
IsDefinedbool IsDefined(Type attributeType, bool inherit)Indicates whether one or more attributes of the specified type are applied to this property.

Examples

using System;
using System.Reflection;

class Sample
{
    public string Name { get; set; }
    private int Id { get; set; }

    static void Main()
    {
        var type = typeof(Sample);
        foreach (PropertyInfo prop in type.GetProperties(
                 BindingFlags.Public | BindingFlags.NonPublic |
                 BindingFlags.Instance))
        {
            Console.WriteLine($"Property: {prop.Name}");
            Console.WriteLine($"  Type: {prop.PropertyType}");
            Console.WriteLine($"  CanRead: {prop.CanRead}");
            Console.WriteLine($"  CanWrite: {prop.CanWrite}");
            Console.WriteLine($"  GetMethod IsPublic: {prop.GetGetMethod()?.IsPublic}");
            Console.WriteLine($"  SetMethod IsPublic: {prop.GetSetMethod()?.IsPublic}");
            Console.WriteLine();
        }
    }
}
Imports System
Imports System.Reflection

Module Sample
    Public Property Name As String
    Private Property Id As Integer

    Sub Main()
        Dim t As Type = GetType(Sample)
        For Each prop As PropertyInfo In t.GetProperties( _
                BindingFlags.Public Or BindingFlags.NonPublic Or _
                BindingFlags.Instance)

            Console.WriteLine($"Property: {prop.Name}")
            Console.WriteLine($"  Type: {prop.PropertyType}")
            Console.WriteLine($"  CanRead: {prop.CanRead}")
            Console.WriteLine($"  CanWrite: {prop.CanWrite}")
            Console.WriteLine($"  GetMethod IsPublic: {If(prop.GetGetMethod() IsNot Nothing, prop.GetGetMethod().IsPublic, "N/A")}")
            Console.WriteLine($"  SetMethod IsPublic: {If(prop.GetSetMethod() IsNot Nothing, prop.GetSetMethod().IsPublic, "N/A")}")
            Console.WriteLine()
        Next
    End Sub
End Module
```