从元数据中获取信息

+展开
-C#
///通过反射从元数据中获取信息
using System;
using System.Reflection;

class wangjun
{
    static void Main()
    {
        //设置字符串为空
        string name2 = string.Empty;
       //得到现在正在使用的应用域的名字
        string name = AppDomain.CurrentDomain.FriendlyName;
        //打印现在应用域的名字
        Console.WriteLine(name);
        //新建应用域的对象,此对象变量指向当前应用域对象
        AppDomain appdomain2 = AppDomain.CurrentDomain;
        //遍历当前应用域中的所有成程序集
        foreach (Assembly asse in appdomain2.GetAssemblies())
        {
            //得到程序集的名字
            name2 = asse.FullName;
            //输出程序集的名字
            Console.WriteLine(asse.FullName);
        }
        //使用Assembly.ReflectionOnlyLoad()方法载入程序集,程序集将仅用
        //于反射,CLR将不允许以这种方法载入的程序集代码执行
        Assembly.ReflectionOnlyLoad(name2);
        //遍历当前应用域中所有的程序集
        foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
        {
            //输出程序集的名称
            Console.WriteLine("\n Assebly:" + a.GetName().Name);

           //遍历程序集中所有的类型
            foreach (Type t in a.GetTypes())
            {
                //如果不是类型,或是不是公共的属性 将跳出遍历
                if (!t.IsClass || !t.IsPublic)
                {
                    continue;
                }

                bool bDerivedException = false;
                bool bDirectInherit = true;

                //得到派生自System.Exception类型的类型并输出,
                //没有派生自System.Exception类型的类型前面标记有星号
                Type baseType = t.BaseType;
                while (baseType!=null&&!bDerivedException)
                {
                    if (baseType == typeof(System.Exception))
                    {
                        bDerivedException = true;
                    }
                    else
                    {
                        bDirectInherit = true;
                    }

                    baseType = baseType.BaseType;
                }
                if (bDerivedException)
                {
                    Console.WriteLine(" " + t);
                }
                else
                {
                    Console.WriteLine(" *" + t);
                }
            }
        }
    }
}

来源:http://2sws.blog.163.com/blog/static/179102492009843458612/

加支付宝好友偷能量挖...


评论(0)网络
阅读(104)喜欢(0)Asp.Net/C#/WCF