PropertyInfo.GetIndexParameters Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Quando viene sottoposto a override in una classe derivata, restituisce una matrice di tutti i parametri di indice per la proprietà.
public:
abstract cli::array <System::Reflection::ParameterInfo ^> ^ GetIndexParameters();
public abstract System.Reflection.ParameterInfo[] GetIndexParameters();
abstract member GetIndexParameters : unit -> System.Reflection.ParameterInfo[]
Public MustOverride Function GetIndexParameters () As ParameterInfo()
Restituisce
Matrice di tipo ParameterInfo
contenente i parametri per gli indici. Se la proprietà non è stata indicizzata, la matrice conterrà 0 (zero) elementi.
Implementazioni
Esempio
Nell'esempio seguente vengono visualizzati i parametri di indice della proprietà specificata.
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 Mypropertyinfo
{
public static int Main()
{
// Get the type and PropertyInfo.
Type t = Type.GetType("MyProperty");
PropertyInfo pi = t.GetProperty("Caption");
// Get the public GetIndexParameters method.
ParameterInfo[] parms = pi.GetIndexParameters();
Console.WriteLine("\r\n" + t.FullName + "." + pi.Name
+ " has " + parms.GetLength(0) + " parameters.");
// Display a property that has parameters. The default
// name of an indexer is "Item".
pi = t.GetProperty("Item");
parms = pi.GetIndexParameters();
Console.WriteLine(t.FullName + "." + pi.Name + " has " +
parms.GetLength(0) + " parameters.");
foreach( ParameterInfo p in parms )
{
Console.WriteLine(" Parameter: " + p.Name);
}
return 0;
}
}
/*
This example produces the following output:
MyProperty.Caption has 0 parameters.
MyProperty.Item has 1 parameters.
Parameter: Index
*/
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"}
Public Default Property Item(ByVal Index As Integer) As String
Get
Return strings(Index)
End Get
Set
strings(Index) = Value
End Set
End Property
End Class
Public Class Example
Public Shared Function Main() 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()
Console.WriteLine(vbCrLf & t.FullName & "." & pi.Name & _
" has " & params.GetLength(0) & " parameters.")
' Display a property that has parameters.
pi = t.GetProperty("Item")
params = pi.GetIndexParameters()
Console.WriteLine(t.FullName & "." & pi.Name & _
" has " & params.GetLength(0) & " parameters.")
For Each p As ParameterInfo In params
Console.WriteLine(" Parameter: " & p.Name)
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
Commenti
Estrarre le informazioni sui parametri necessarie dalla matrice restituita.
Per usare il GetIndexParameters
metodo , ottenere prima di tutto la classe Type
. Da ottenere l'oggetto Type
PropertyInfo
.
PropertyInfo
Da , usare il GetIndexParameters
metodo .