Csharp/C Sharp by API/System.Reflection/PropertyInfo

Материал из .Net Framework эксперт
Версия от 12:11, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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);
                }
            }
        }
    }
}


PropertyInfo.MemberType

  
using System;
using System.Reflection;
class MainClass {
    public static void Main(string[] args) {
        ShowClasses(args[0]);
    }
    public static void ShowMethods(Type t) {
        MethodInfo[] methods = t.GetMethods();
        foreach (MethodInfo m in methods) {
            Console.WriteLine("\nMethod Name: {0}", m.Name);
            Console.WriteLine("Return Type: {0}", m.ReturnType);
        }
    }
    public static void ShowProperties(Type t) {
        PropertyInfo[] props = t.GetProperties();
        foreach (PropertyInfo p in props) {
            Console.WriteLine("\nProperty Name: {0}", p.Name);
            Console.WriteLine("Type: {0}", p.MemberType);
        }
    }
    public static void ShowClasses(string name) {
        Assembly assembly = Assembly.LoadFrom(name);
        if (assembly != null) {
            Type[] typeArray = assembly.GetTypes();
            Console.WriteLine("Assembly Name: {0}", name);
            foreach (Type type in typeArray) {
                if (type.IsClass) {
                    Console.WriteLine("Class: {0}", type.FullName);
                    ShowMethods(type);
                    ShowProperties(type);
                }
            }
        }
    }
}


PropertyInfo.Name

  
using System;
using System.Reflection;
class MainClass {
    public static void Main(string[] args) {
        ShowClasses(args[0]);
    }
    public static void ShowMethods(Type t) {
        MethodInfo[] methods = t.GetMethods();
        foreach (MethodInfo m in methods) {
            Console.WriteLine("\nMethod Name: {0}", m.Name);
            Console.WriteLine("Return Type: {0}", m.ReturnType);
        }
    }
    public static void ShowProperties(Type t) {
        PropertyInfo[] props = t.GetProperties();
        foreach (PropertyInfo p in props) {
            Console.WriteLine("\nProperty Name: {0}", p.Name);
            Console.WriteLine("Type: {0}", p.MemberType);
        }
    }
    public static void ShowClasses(string name) {
        Assembly assembly = Assembly.LoadFrom(name);
        if (assembly != null) {
            Type[] typeArray = assembly.GetTypes();
            Console.WriteLine("Assembly Name: {0}", name);
            foreach (Type type in typeArray) {
                if (type.IsClass) {
                    Console.WriteLine("Class: {0}", type.FullName);
                    ShowMethods(type);
                    ShowProperties(type);
                }
            }
        }
    }
}