PropertyInfo.GetIndexParameters 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当在派生类中重写时,返回此属性的所有索引参数的数组。
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()
返回
ParameterInfo
类型的数组,它包含索引的参数。 如果未为该属性编制索引,则数组包含 0(零)个元素。
实现
示例
以下示例显示指定属性的索引参数。
using namespace System;
using namespace System::Reflection;
// A class that contains some properties.
public ref class MyProperty
{
private:
// Define a simple string property.
String^ caption;
public:
property String^ Caption
{
String^ get()
{
return caption;
}
void set( String^ value )
{
if ( caption != value )
{
caption = value;
}
}
}
private:
// A very limited indexer that gets or sets one of four
// strings.
array<String^>^strings;
public:
MyProperty()
{
array<String^>^temp0 = {"abc","def","ghi","jkl"};
strings = temp0;
}
property String^ Item [int]
{
String^ get( int Index )
{
return strings[ Index ];
}
void set( int Index, String^ value )
{
strings[ Index ] = value;
}
}
};
int main()
{
// Get the type and PropertyInfo.
Type^ t = Type::GetType( "MyProperty" );
PropertyInfo^ pi = t->GetProperty( "Caption" );
// Get the public GetIndexParameters method.
array<ParameterInfo^>^parms = pi->GetIndexParameters();
Console::WriteLine( "\n{0}.{1} has {2} parameters.", t->FullName, pi->Name, parms->GetLength( 0 ) );
// Display a property that has parameters.
pi = t->GetProperty( "Item" );
parms = pi->GetIndexParameters();
Console::WriteLine( "{0}.{1} has {2} parameters.", t->FullName, pi->Name, parms->GetLength( 0 ) );
for ( int i = 0; i < parms->GetLength( 0 ); i++ )
{
Console::WriteLine( " Parameter: {0}", parms[ i ]->Name );
}
return 0;
}
/*
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 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
注解
从返回的数组中提取任何必需的参数信息。
若要使用 GetIndexParameters
方法,请先获取类 Type
。
Type
从 中获取 PropertyInfo
。 在 中 PropertyInfo
,使用 GetIndexParameters
方法。