C#反射類型的使用

來源:互聯網
上載者:User

using System;
using System.Security.Permissions;
using System.Text;
using System.Reflection;
// Download by http://www.codefans.net
namespace UseReflection
{
    /// <summary>
    /// 這裡的特性要求程式的運行需要完全信任安全層級
    /// </summary>
    [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
    partial class UseReflection
    {
        static void Main(string[] args)
        {
            //需要根據代碼位置進行更新
            //對程式集進行遍曆分析
            Assembly assembly = Assembly.LoadFrom(@"..\..\..\..\6-8 簡單程式集\SimpleAssembly\bin\Debug\SimpleAssembly.exe");
            AnalyzeAssembly(assembly);

            //建立一個程式集中的類型的對象
            //這裡嘗試建立SimpleAssembly對象
            Console.WriteLine("利用反射建立類型");
            String[] pars={"測試反射"};
            Object o = assembly.CreateInstance(assembly.GetModules()[0].GetTypes()[0].ToString(),
                true, BindingFlags.CreateInstance, null, pars, null, null);
            Console.WriteLine(o);
            Console.Read();
        }
    }
    partial class UseReflection
    {
        /// <summary>
        /// 對一個程式集進行分析
        /// </summary>
        /// <param name="assembly"></param>
        private static void AnalyzeAssembly(Assembly assembly)
        {
            Console.WriteLine("程式集名字:"+assembly.FullName);
            Console.WriteLine("程式集位置:" + assembly.Location);
            Console.WriteLine("程式集是否在GAC中:" +
                        assembly.GlobalAssemblyCache.ToString());
            Console.WriteLine("包含程式集的模組名" +
                assembly.ManifestModule.Name);
            Console.WriteLine("運行程式集需要的CLR版本:"+
                assembly.ImageRuntimeVersion);
            Console.WriteLine("現在開始剖析器集中的模組");
            Module[] modules = assembly.GetModules();
            foreach (Module module in modules)
                AnalyzeModule(module);
            ////遞迴分析引用程式集
            ////這裡的代碼供讀者參考
            //Console.WriteLine("開始分析引用的程式集");
            //AssemblyName[] refassemblies = assembly.GetReferencedAssemblies();
            //foreach (AssemblyName refname in refassemblies)
            //{
            //    Assembly refassembly = Assembly.Load(refname);
            //    AnalyzeAssembly(refassembly);
            //}
        }
    }
    partial class UseReflection
    {
        /// <summary>
        /// 對一個模組進行分析
        /// </summary>
        /// <param name="module">模組</param>
        private static void AnalyzeModule(Module module)
        {
            Console.WriteLine("模組名:" + module.Name);
            Console.WriteLine("模組的UUID:" + module.ModuleVersionId);
            Console.WriteLine("開始分析模組下的類型");
            Type[] types = module.GetTypes();
            foreach (Type type in types)
                AnalyzeType(type);
        }
    }
    partial class UseReflection
    {
        /// <summary>
        /// 對一個類型進行分析
        /// </summary>
        /// <param name="type">類型</param>
        private static void AnalyzeType(Type type)
        {
            Console.WriteLine("類型名字:" + type.Name);
            Console.WriteLine("類型的類別是:"+type.Attributes);
            if (type.BaseType != null)
                Console.WriteLine("類型的基類是:" + type.BaseType.Name);
            Console.WriteLine("類型的GUID是:" + type.GUID);
            //設定感興趣的類型成員
            BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
                            BindingFlags.Static | BindingFlags.Instance);
            //分析成員
            FieldInfo[] fields = type.GetFields(flags);
            if (fields.Length > 0)
            {
                Console.WriteLine("開始分析類型的成員");
                foreach (FieldInfo field in fields)
                    AnalyzeField(field);
            }
            //分析包含的方法
            MethodInfo[] methods = type.GetMethods(flags);
            if (methods.Length > 0)
            {
                Console.WriteLine("開始分析類型的方法");
                foreach (MethodInfo method in methods)
                    AnalyzeMethod(method);
            }
            //分析屬性
            PropertyInfo[] properties = type.GetProperties(flags);
            if (properties.Length > 0)
            {
                Console.WriteLine("開始分析類型的屬性");
                foreach (PropertyInfo property in properties)
                    AnalyzeProperty(property);
            }
        }
    }
    partial class UseReflection
    {
        /// <summary>
        /// 對一個成員進行分析,這裡只做簡單的分析
        /// 可以進一步分析成員的可存取層級,
        /// 或通過成員得到包含它的類型、模組等
        /// </summary>
        /// <param name="field">成員</param>
        private static void AnalyzeField(FieldInfo field)
        {
            Console.WriteLine("成員名字:" + field.Name);
            Console.WriteLine("成員的類別:" + field.Attributes);
            Console.WriteLine("成員的類型名:" + field.FieldType.Name);
        }
        /// <summary>
        /// 對一個方法進行分析
        /// </summary>
        /// <param name="method"></param>
        private static void AnalyzeMethod(MethodInfo method)
        {
            Console.WriteLine("方法名字:" + method.Name);
            Console.WriteLine("方法的類別:" + method.Attributes);
            Console.WriteLine("開始分析方法的參數");
            ParameterInfo[] parameters = method.GetParameters();
            if (parameters.Length <= 0)
                Console.WriteLine("方法沒有參數");
            foreach (ParameterInfo parameter in parameters)
                AnalyzeParameter(parameter);
            ParameterInfo retpar = method.ReturnParameter;
            Console.WriteLine("分析方法的返回參數");
            AnalyzeParameter(retpar);
        }
        /// <summary>
        /// 分析方法參數
        /// </summary>
        /// <param name="parameter"></param>
        private static void AnalyzeParameter(ParameterInfo parameter)
        {
            Console.WriteLine("參數名字:" + parameter.Name);
            Console.WriteLine("參數的類別:" + parameter.Attributes);
            Console.WriteLine("參數的類型:" + parameter.ParameterType.Name);
        }
        /// <summary>
        /// 分析屬性
        /// </summary>
        /// <param name="property"></param>
        private static void AnalyzeProperty(PropertyInfo property)
        {
            Console.WriteLine("屬性名稱字:" + property.Name);
            Console.WriteLine("屬性的類別:" + property.Attributes);
            Console.WriteLine("是否可讀:" + property.CanRead.ToString());
            Console.WriteLine("是否可寫:" + property.CanWrite.ToString());
        }
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.