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
Name | Type | Description |
---|---|---|
CanRead | bool | Gets a value indicating whether the property can be read. |
CanWrite | bool | Gets a value indicating whether the property can be written to. |
PropertyType | Type | Gets the type of the property. |
GetMethod | MethodInfo | Gets the MethodInfo object for the property's get accessor. |
SetMethod | MethodInfo | Gets the MethodInfo object for the property's set accessor. |
Attributes | PropertyAttributes | Gets the attributes for this property. |
IsSpecialName | bool | Indicates whether the property has a specialname attribute. |
ReflectedType | Type | Gets the class that was used to obtain this PropertyInfo object. |
DeclaringType | Type | Gets the class that declares this property. |
Methods
Name | Signature | Description |
---|---|---|
GetValue | object GetValue(object obj, object[] index) | Gets the value of the property for a given object. |
SetValue | void SetValue(object obj, object value, object[] index) | Sets the value of the property for a given object. |
GetGetMethod | MethodInfo GetGetMethod(bool nonPublic) | Returns the public or non‑public get accessor. |
GetSetMethod | MethodInfo GetSetMethod(bool nonPublic) | Returns the public or non‑public set accessor. |
GetCustomAttributes | object[] GetCustomAttributes(bool inherit) | Returns an array of custom attributes applied to this property. |
IsDefined | bool 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