Get the public property with the specified name in CSharp

Description

The following code shows how to get the public property with the specified name.

Example


using System;//  www  .  j av a2 s. c  om
using System.Reflection;

class MyClass
{
    private int myProperty;
    public int MyProperty
    {
        get
        {
            return myProperty;
        }
        set
        {
            myProperty=value;
        }
    }
}
public class MyTypeClass
{
    public static void Main(string[] args)
    {
        Type myType=typeof(MyClass);       
        PropertyInfo myPropInfo = myType.GetProperty("MyProperty");
        Console.WriteLine("The {0} property exists in MyClass.", myPropInfo.Name);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Reflection »




Array
Constructor
Event
Field
Interface
Method
Properties
Type