Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Type.GetProperty Method (String, Type[])

.NET Framework 1.1

Searches for the specified public property whose parameters match the specified argument types.

[Visual Basic]
Overloads Public Function GetProperty( _
   ByVal name As String, _
   ByVal types() As Type _
) As PropertyInfo
[C#]
public PropertyInfo GetProperty(
 string name,
 Type[] types
);
[C++]
public: PropertyInfo* GetProperty(
 String* name,
 Type* types[]
);
[JScript]
public function GetProperty(
   name : String,
 types : Type[]
) : PropertyInfo;

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.

-or-

An empty array of the type Type (that is, Type[] types = new Type[0]) to get a property that is not indexed.

Return Value

A PropertyInfo object representing the public property whose parameters match the specified argument types, if found; otherwise, a null reference (Nothing in Visual Basic).

Exceptions

Exception Type Condition
AmbiguousMatchException More than one property is found with the specified name and matching the specified argument types.
ArgumentNullException name is a null reference (Nothing in Visual Basic).

-or-

types is a null reference (Nothing).

-or-

One of the elements in types is a null reference (Nothing).

ArgumentException types is multidimensional.

Remarks

The search for name is case-sensitive.

If the requested type is non-public and the caller does not have ReflectionPermission to reflect non-public objects outside the current assembly, this method returns a null reference (Nothing in Visual Basic).

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Imports System
Imports System.Reflection

Module Module1
    Class MyClass1
        Private myArray As Integer(,) = {{1, 2}, {3, 4}}
        ' Declare an indexer.
        Default Public Property Item(ByVal i As Integer, ByVal j As Integer) As Integer
            Get
                Return myArray(i, j)
            End Get
            Set(ByVal Value As Integer)

                myArray(i, j) = Value
            End Set
        End Property
    End Class 'MyClass1

    Public Class MyTypeClass
        Public Shared Sub Main()
            Try
                ' Get the Type Object.
                Dim myType As Type = GetType(MyClass1)
                Dim myTypeArr(1) As Type
                ' Create an instance of a Type array.
                myTypeArr.SetValue(GetType(Integer), 0)
                myTypeArr.SetValue(GetType(Integer), 1)
                ' Get the PropertyInfo object for the indexed property Item, which has two integer parameters. 
                Dim myPropInfo As PropertyInfo = myType.GetProperty("Item", myTypeArr)
                ' Display the property.
                Console.WriteLine("The {0} property exists in MyClass1.", myPropInfo.ToString())
            Catch e As NullReferenceException
                Console.WriteLine("An exception occurred.")
                Console.WriteLine("Source : {0}", e.Source.ToString())
                Console.WriteLine("Message : {0}", e.Message.ToString())
            End Try
        End Sub 'Main
    End Class 'MyTypeClass
End Module 'Module1

[C#] 

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)
    {
        try
        { 
            // Get the Type object.
            Type myType=typeof(MyClass1);       
            Type[] myTypeArr = new Type[2];
            // Create an instance of a Type array.
            myTypeArr.SetValue(typeof(int),0);            
            myTypeArr.SetValue(typeof(int),1);
            // Get the PropertyInfo object for the indexed property Item, which has two integer parameters. 
            PropertyInfo myPropInfo = myType.GetProperty("Item", myTypeArr);
            // Display the property.
            Console.WriteLine("The {0} property exists in MyClass1.", myPropInfo.ToString());
        }           
        catch(NullReferenceException e)
        {
            Console.WriteLine("An exception occurred.");
            Console.WriteLine("Source : {0}" , e.Source);
            Console.WriteLine("Message : {0}" , e.Message);
        }
    }
}

[C++] 

#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;

__gc class MyClass1 {
private:
   int myArray[,];

   // Declare an indexer.
public:
   __property int get_Item (int i, int j) {
      return myArray[i, j];
   }
   __property void set_Item(int i, int j, int value) {
      myArray[i, j] = value;
   }
};

int main() {
   try {
      // Get the Type object.
      Type* myType=__typeof(MyClass1);
      Type* myTypeArr[] = new Type*[2];
      // Create an instance of a Type array.
      myTypeArr->SetValue(__typeof(int), 0);
      myTypeArr->SetValue(__typeof(int), 1);
      // Get the PropertyInfo object for the indexed property Item, which has two integer parameters.
      PropertyInfo*  myPropInfo = myType->GetProperty(S"Item", myTypeArr);
      // Display the property.
      Console::WriteLine(S"The {0} property exists in MyClass1.", myPropInfo);
   } catch (NullReferenceException* e) {
      Console::WriteLine(S"An exception occurred.");
      Console::WriteLine(S"Source : {0}" , e->Source);
      Console::WriteLine(S"Message : {0}" , e->Message);
   }
}

[Visual Basic, C#, C++] Internally, this property is referred to by the name "Item" in the metadata. Therefore, any attempt to get PropertyInfo using reflection must specify this internal name in order for the PropertyInfo to be returned correctly.

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

Type Class | Type Members | System Namespace | Type.GetProperty Overload List | PropertyInfo | String | DefaultBinder | ReflectionPermission | GetPropertyImpl | GetProperties

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.