Csharp/C Sharp/Reflection/PropertyInfo — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

IsClass, Namespace, FullName, IsAbstract, IsPublic, IsInterface, IsEnum

 
using System;
using System.Reflection;
class MainClass {
    public static void ShowTypes(string name, Assembly assembly) {
        Type[] typeArray = assembly.GetTypes();
        Console.WriteLine(name);
        foreach (Type type in typeArray) {
            if (type.IsClass) {
                Console.WriteLine("\nNamespace : {0}", type.Namespace);
                Console.WriteLine("Class : {0}", type.FullName);
                if (type.BaseType != null)
                    Console.WriteLine("Base Class : {0}",
                      type.BaseType.FullName);
                else
                    Console.WriteLine("Class not derived from anything");
                if (type.IsAbstract)
                    Console.WriteLine("Abstract base class");
                else
                    Console.WriteLine("Instantiable class");
                if (type.IsPublic)
                    Console.WriteLine("Scope: Public");
                else
                    Console.WriteLine("Scope: Private");
            } else if (type.IsInterface) {
                    Console.WriteLine("\nNamespace : {0}", type.Namespace);
                    Console.WriteLine("Interface : {0}", type.FullName);
                    if (type.IsPublic)
                        Console.WriteLine("Scope: Public");
                    else
                        Console.WriteLine("Scope: Private");
                } else
                    if (type.IsEnum) {
                        Console.WriteLine("\nEnumeration: {0}", type.FullName);
                    } else
                        Console.WriteLine("\nType: {0}", type.FullName);
        }
    }
    public static void Main(string[] args) {
        for (int i = 0; i < args.Length; ++i) {
            Assembly assembly = Assembly.LoadFrom(args[0]);
            ShowTypes(args[0], assembly);
        }
    }
}


Obtaining member information from a class.

 

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


Print Property Info

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


Type.GetProperties

 
//ReflectionTest
using System;
using System.Reflection;
public class Class1 {
    public static int Main() {
        Type t = typeof(MyClass);
        Console.WriteLine("Type of class: " + t);
        Console.WriteLine("Namespace: " + t.Namespace);
        ConstructorInfo[] ci = t.GetConstructors();
        PropertyInfo[] pi = t.GetProperties();
        Console.WriteLine("Properties are:");
        foreach (PropertyInfo i in pi) {
            Console.WriteLine(i);
        }
        return 0;
    }

    public class MyClass {
        public int pubInteger;
        private int _privValue;
        public MyClass() {
        }
        public MyClass(int IntegerValueIn) {
            pubInteger = IntegerValueIn;
        }
        public int Add10(int IntegerValueIn) {
            Console.WriteLine(IntegerValueIn);
            return IntegerValueIn + 10;
        }
        public int TestProperty {
            get {
                return _privValue;
            }
            set {
                _privValue = value;
            }
        }
    }
}