Sdílet prostřednictvím


PropertyInfo.GetIndexParameters Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

When overridden in a derived class, returns an array of all the index parameters for the property.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public MustOverride Function GetIndexParameters As ParameterInfo()
public abstract ParameterInfo[] GetIndexParameters()

Return Value

Type: array<System.Reflection.ParameterInfo[]
An array that contains the parameters for the indexes. If the property is not indexed, the array has 0 (zero) elements.

Exceptions

Exception Condition
MethodAccessException

Application code attempts to access this member late-bound, for example by using the Type.InvokeMember method.

Remarks

Extract any required parameter information from the returned array.

To use the GetIndexParameters method, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, use the GetIndexParameters method.

Examples

The following example displays the number of index parameters of two properties, one that has an index and one that does not.

Imports System.Reflection
Imports System.Collections

' A test class that has some properties.
Public Class MyProperty

   ' Define a simple string property.
   Private myCaption As String = "A Default caption"
   Public Property Caption() As String
      Get
         Return myCaption
      End Get
      Set(ByVal Value As String)
         If myCaption <> Value Then
            myCaption = Value
         End If
      End Set
   End Property

   ' A very limited indexed default property that gets or
   ' sets one of four string values.
   Private strings() As String = {"abc", "def", "ghi", "jkl"}
   Default Public Property Item(ByVal Index As Integer) As String
      Get
         Return strings(Index)
      End Get
      Set(ByVal value As String)
         strings(Index) = value
      End Set
   End Property
End Class

Public Class Example

   Public Shared Function Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) As Integer

      ' Get the type and PropertyInfo.
      Dim t As Type = GetType(MyProperty)
      Dim pi As PropertyInfo = t.GetProperty("Caption")

      ' Get an array containing the parameters (if any).
      Dim params As ParameterInfo() = pi.GetIndexParameters()
      outputBlock.Text &= vbCrLf & t.FullName & "." & pi.Name & _
         " has " & params.GetLength(0) & " parameters." & vbCrLf

      ' Display a property that has parameters.
      pi = t.GetProperty("Item")
      params = pi.GetIndexParameters()
      outputBlock.Text &= t.FullName & "." & pi.Name & _
         " has " & params.GetLength(0) & " parameters." & vbCrLf
      For Each p As ParameterInfo In params
         outputBlock.Text &= "   Parameter: " & p.Name & vbCrLf
      Next

      Return 0
   End Function
End Class

' This example produces the following output:
' MyProperty.Caption has 0 parameters.
' MyProperty.Item has 1 parameters.
'    Parameter: Index
using System;
using System.Reflection;

// A class that contains some properties.
public class MyProperty
{
   // Define a simple string property.
   private string caption = "A Default caption";
   public string Caption
   {
      get { return caption; }
      set
      {
         if (caption != value) { caption = value; }
      }
   }

   // A very limited indexer that gets or sets one of four 
   // strings.
   private string[] strings = { "abc", "def", "ghi", "jkl" };
   public string this[int Index]
   {
      get
      {
         return strings[Index];
      }
      set
      {
         strings[Index] = value;
      }
   }
}

class Example
{
   public static int Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Get the type and PropertyInfo.
      Type t = Type.GetType("MyProperty");
      PropertyInfo pi = t.GetProperty("Caption");

      // Get the public GetIndexParameters method.
      ParameterInfo[] parms = pi.GetIndexParameters();
      outputBlock.Text += "\r\n" + t.FullName + "." + pi.Name
          + " has " + parms.GetLength(0) + " parameters." + "\n";

      // Display a property that has parameters. The default 
      // name of an indexer is "Item".
      pi = t.GetProperty("Item");
      parms = pi.GetIndexParameters();
      outputBlock.Text += t.FullName + "." + pi.Name + " has " +
          parms.GetLength(0) + " parameters." + "\n";
      foreach (ParameterInfo p in parms)
      {
         outputBlock.Text += "   Parameter: " + p.Name + "\n";
      }

      return 0;
   }
}
/*
 This example produces the following output:
 MyProperty.Caption has 0 parameters.
 MyProperty.Item has 1 parameters.
    Parameter: Index
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.