当前位置: 首页>>代码示例>>C#>>正文


C# PropertyInfo.GetIndexParameters方法代码示例

本文整理汇总了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;
    }
}
开发者ID:.NET开发者,项目名称:System.Reflection,代码行数:59,代码来源:PropertyInfo.GetIndexParameters

输出:

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);
                }
            }
        }
    }
}
开发者ID:C#程序员,项目名称:System.Reflection,代码行数:42,代码来源:PropertyInfo.GetIndexParameters


注:本文中的System.Reflection.PropertyInfo.GetIndexParameters方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。