C# Type GetProperty(String, Type)

Description

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

Syntax

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


public PropertyInfo GetProperty(
  string name,
  Type returnType
)

Parameters

Type.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

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

Example

The following example defines a class with one property and retrieves the name and type of the property.


using System;//from w ww.ja  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 »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version