NET反射实例代码_踏浪帅的空间_百度空间


using System.Reflection;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("***** Welcome to MyTypeViewer *****");
            string typeName = "";
            bool userIsDone = false;

            #region Print the type infomation
            do
            {
                Console.WriteLine("\nEnter a type name to evaluate");
                Console.Write("or enter Q to quit: ");

                // Get name of type.得到类型的名称
                typeName = Console.ReadLine();

                // Does user want to quit?询问用户是否想退出
                if (typeName.ToUpper() == "Q")
                {
                    userIsDone = true;
                    break;
                }

                // Try to get named type.测试显示类型
                try
                {
                    Type t = Type.GetType(typeName);
                    Console.WriteLine("");
                    ListVariousStats(t);
                    ListFields(t);
                    ListProps(t);
                    ListMethods(t);
                    ListInterfaces(t);
                }
                catch
                {
                    Console.WriteLine("Sorry, can't find type");
                }
            } while (!userIsDone);
            #endregion
        }

        #region Static helper methods

        // Just for good measure. 为了更好的检测
        public static void ListVariousStats(Type t)
        {
            Console.WriteLine("***** Various Statistics *****");
            Console.WriteLine("Base class is: {0}", t.BaseType);
            Console.WriteLine("Is type abstract? {0}", t.IsAbstract);
            Console.WriteLine("Is type sealed? {0}", t.IsSealed);
            Console.WriteLine("Is type generic? {0}", t.IsGenericTypeDefinition);
            Console.WriteLine("Is type a class type? {0}", t.IsClass);
            Console.WriteLine("");
        }

        // Display method names of type. 显示类型的方法名称      
        public static void ListMethods(Type t)
        {
            Console.WriteLine("***** Methods *****");
            MethodInfo[] mi = t.GetMethods();//把它放入一个反射方法集合里
            foreach (MethodInfo m in mi)
            {
                // Get return value.得到返回值
                string retVal = m.ReturnType.FullName;
                string paramInfo = "(";

                // Get params.得到参数
                foreach (ParameterInfo pi in m.GetParameters())
                {
                    paramInfo += string.Format("{0} {1} ", pi.ParameterType, pi.Name);
                }
                paramInfo += ")";

                // Now display the basic method sig. 现在显示基本方法
                Console.WriteLine("->{0} {1} {2}", retVal, m.Name, paramInfo);
            }
            Console.WriteLine("");
        }

        // Display field names of type. 显示类型的字段名
        public static void ListFields(Type t)
        {
            Console.WriteLine("***** Fields *****");
            FieldInfo[] fi = t.GetFields();
            foreach (FieldInfo field in fi)
                Console.WriteLine("->{0}", field.Name);
            Console.WriteLine("");
        }

        // Display property names of type. 显示类型的属性名称
        public static void ListProps(Type t)
        {
            Console.WriteLine("***** Properties *****");
            PropertyInfo[] pi = t.GetProperties();
            foreach (PropertyInfo prop in pi)
                Console.WriteLine("->{0}", prop.Name);
            Console.WriteLine("");
        }

        // Display implemented interfaces.
        public static void ListInterfaces(Type t)//显示接口
        {
            Console.WriteLine("***** Interfaces *****");
            Type[] ifaces = t.GetInterfaces();
            foreach (Type i in ifaces)
                Console.WriteLine("->{0}", i.Name);
        }
        #endregion
    }
}

运行结果:






郑重声明:资讯 【NET反射实例代码_踏浪帅的空间_百度空间】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——