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

Description

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

Syntax

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


public PropertyInfo GetProperty(
  string name,
  Type[] types
)

Parameters

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

  • name - The string containing the name of the public property to get.
  • 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.

Returns

Type.GetProperty(String, Type[]) method returns An object representing the public property whose parameters match the specified argument types, if found; otherwise, null.

Example

The following example retrieves the Type object of a user-defined class, retrieves the property of that class, and displays the property name and type of the property as specified by the arguments passed to GetProperty.


//from  w  w w.j a v a 2  s.c  o m
using System;
using System.Reflection;
class MyClass1
{         
    private int [,] myArray = {{1,2},{3,4}}; 
    // Declare an indexer. 
    public int this [int i,int j]   
    {
        get 
        {
            return myArray[i,j];
        }
        set 
        {
            myArray[i,j] = value;
        }
    }
}
public class MyTypeClass
{
    public static void Main(string[] args)
    {
            Type myType=typeof(MyClass1);       
            Type[] myTypeArr = new Type[2];

            myTypeArr.SetValue(typeof(int),0);            
            myTypeArr.SetValue(typeof(int),1);
            PropertyInfo myPropInfo = myType.GetProperty("Item", myTypeArr);
            Console.WriteLine("The {0} property exists in MyClass1.", myPropInfo.ToString());
    }
}

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