Get public property with the specified name and return type in CSharp

Description

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

Example


using System;/* w  ww.  j a v a2 s  .  c  o m*/
using System.Reflection;

class MyClass1
{
    String myMessage="Hello World.";
    public string MyProperty1
    {
        get
        {      
            return myMessage;
        }
        set
        {
            myMessage =value;
        }      
    }
}
class TestClass
{
    static void Main()
    {
        Type myType = typeof(MyClass1);
        PropertyInfo myStringProperties1 = myType.GetProperty("MyProperty1",typeof(string));
        Console.WriteLine("The name of the first property of MyClass1 is {0}.", myStringProperties1.Name);
        Console.WriteLine("The type of the first property of MyClass1 is {0}.", myStringProperties1.PropertyType);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Reflection »




Array
Constructor
Event
Field
Interface
Method
Properties
Type