C# Type GetProperty(String, Type, Type[], ParameterModifier[])

Description

Type GetProperty(String, Type, Type[], ParameterModifier[]) searches for the specified public property whose parameters match the specified argument types and modifiers.

Syntax

Type.GetProperty(String, Type, Type[], ParameterModifier[]) has the following syntax.


public PropertyInfo GetProperty(
  string name,/* w  w w.jav a 2s.com*/
  Type returnType,
  Type[] types,
  ParameterModifier[] modifiers
)

Parameters

Type.GetProperty(String, Type, Type[], ParameterModifier[]) has the following parameters.

  • name - The string containing the name of the public property to get.
  • returnType - The return type of the property.
  • types - An array of Type objects representing the number, order, and type of the parameters for the indexed property to get.
  • types - -or-
  • types - An empty array of the type Type (that is, Type[] types = new Type[0]) to get a property that is not indexed.
  • modifiers - An array of ParameterModifier objects representing the attributes associated with the corresponding element in the types array. The default binder does not process this parameter.

Returns

Type.GetProperty(String, Type, Type[], ParameterModifier[]) method returns An object representing the public property that matches the specified requirements, if found; otherwise, null.

Example

The following example obtains a Type object corresponding to MyPropertyClass, and the indexed property of this class is retrieved using the arguments passed to the GetProperty method.


using System;/*from  w w  w  . j av  a  2 s .c  o  m*/
using System.Reflection;
public class MyPropertyClass
{
    private int [,] myPropertyArray = new int[10,10]; 
    // Declare an indexer. 
    public int this [int i,int j]
    {
        get 
        {
            return myPropertyArray[i,j];
        }
        set 
        {
            myPropertyArray[i,j] = value;
        }
    }
}
public class MyTypeClass
{
    public static void Main()
    {
            Type myType=typeof(MyPropertyClass);
            Type[] myTypeArray = new Type[2];

            myTypeArray.SetValue(typeof(int),0);
            myTypeArray.SetValue(typeof(int),1);

            PropertyInfo myPropertyInfo = myType.GetProperty("Item",
                typeof(int),myTypeArray,null);
            Console.WriteLine(myType.FullName + "." + myPropertyInfo.Name + 
                " has a property type of " + myPropertyInfo.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