本文整理汇总了C#中System.Reflection.PropertyInfo.GetIndexParameters方法的典型用法代码示例。如果您正苦于以下问题:C# PropertyInfo.GetIndexParameters方法的具体用法?C# PropertyInfo.GetIndexParameters怎么用?C# PropertyInfo.GetIndexParameters使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.PropertyInfo
的用法示例。
在下文中一共展示了PropertyInfo.GetIndexParameters方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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;
}
}
输出:
MyProperty.Caption has 0 parameters. MyProperty.Item has 1 parameters. Parameter: Index
示例2: PropertyInfo.GetIndexParameters()
//引入命名空间
using System;
using System.Collections;
using System.Reflection;
public class MainClass{
public static void Main() {
Assembly LoadedAsm = Assembly.LoadFrom("yourName");
Console.WriteLine(LoadedAsm.FullName);
Type[] LoadedTypes = LoadedAsm.GetTypes();
if (LoadedTypes.Length == 0) {
Console.WriteLine("\tNo Types!");
} else {
foreach (Type t in LoadedTypes) {
if (t.IsPublic && !t.IsEnum && !t.IsValueType) {
Console.WriteLine("Type: {0}", t.FullName);
PrintPropertyInfo(t);
}
}
}
}
private static void PrintPropertyInfo(Type t) {
PropertyInfo[] props = t.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
foreach (PropertyInfo p in props) {
Console.WriteLine("Property: {0}", p.Name);
Console.WriteLine("\tType {0}", p.PropertyType.FullName);
if (p.CanRead)
Console.WriteLine("Readable property.");
if (p.CanWrite)
Console.WriteLine("Writable property.");
ParameterInfo[] pList = p.GetIndexParameters();
if (pList.Length == 0) {
Console.WriteLine("\tNo Parameters");
} else {
Console.WriteLine("\tParameter List:");
foreach (ParameterInfo parm in pList) {
Console.WriteLine("\t\t{0} {1}", parm.ParameterType.FullName,parm.Name);
}
}
}
}
}