PropertyInfo.GetIndexParameters 方法

定義

當在衍生類別中覆寫時,傳回屬性的所有索引參數的陣列。

C#
public abstract System.Reflection.ParameterInfo[] GetIndexParameters();

傳回

ParameterInfo 類型的陣列,包含索引的參數。 如果此屬性未建立索引,則表示陣列有 0 (零) 個項目。

實作

範例

下列範例會顯示指定之屬性的索引參數。

C#
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
 */

備註

從傳回的陣列擷取任何必要的參數資訊。

若要使用 GetIndexParameters 方法,請先取得 類別 Type。 從取得 TypePropertyInfo。 從使用 PropertyInfoGetIndexParameters 方法。

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0