C# TypeInfo GetProperty(String, Type)

Description

TypeInfo GetProperty(String, Type) Searches for the public property with the specified name and return type.

Syntax

TypeInfo.GetProperty(String, Type) has the following syntax.


public PropertyInfo GetProperty(
  string name,
  Type returnType
)

Parameters

TypeInfo.GetProperty(String, Type) has the following parameters.

  • name - The string containing the name of the public property to get.
  • returnType - The return type of the property.

Returns

TypeInfo.GetProperty(String, Type) method returns An object representing the public property with the specified name, if found; otherwise, null.

Example

Searches for the public property with the specified name and return type.


using System;//w w  w .  java  2  s . com
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 »
    System.Reflection »




EventInfo
FieldInfo
MemberInfo
MethodInfo
ParameterInfo
TypeInfo