C#反射

C# 反射(Reflection)

反射指程序可以訪問、檢測和修改它本身狀態(tài)或行為的一種能力。
程序集包含模塊,而模塊包含類型,類型又包含成員。反射則提供了封裝程序集、模塊和類型的對象。
您可以使用反射動態(tài)地創(chuàng)建類型的實(shí)例,將類型綁定到現(xiàn)有對象,或從現(xiàn)有對象中獲取類型。然后,可以調(diào)用類型的方法或訪問其字段和屬性。
優(yōu)缺點(diǎn)

優(yōu)點(diǎn):
1、反射提高了程序的靈活性和擴(kuò)展性。
2、降低耦合性,提高自適應(yīng)能力。
3、它允許程序創(chuàng)建和控制任何類的對象,無需提前硬編碼目標(biāo)類。

缺點(diǎn):
1、性能問題:使用反射基本上是一種解釋操作,用于字段和方法接入時要遠(yuǎn)慢于直接代碼。因此反射機(jī)制主要應(yīng)用在對靈活性和拓展性要求很高的系統(tǒng)框架上,普通程序不建議使用。
2、使用反射會模糊程序內(nèi)部邏輯;程序員希望在源代碼中看到程序的邏輯,反射卻繞過了源代碼的技術(shù),因而會帶來維護(hù)的問題,反射代碼比相應(yīng)的直接代碼更復(fù)雜。

項(xiàng)目中用到 做個筆記。

public static Type GetType(string TypeName)
    {

        // Try Type.GetType() first. This will work with types defined
        // by the Mono runtime, in the same assembly as the caller, etc.
        var type = Type.GetType(TypeName);

        // If it worked, then we're done here
        if (type != null)
            return type;

        // If the TypeName is a full name, then we can try loading the defining assembly directly
        if (TypeName.Contains("."))
        {

            // Get the name of the assembly (Assumption is that we are using 
            // fully-qualified type names)
            var assemblyName = TypeName.Substring(0, TypeName.IndexOf('.'));

            // Attempt to load the indicated Assembly
            var assembly = Assembly.Load(assemblyName);
            if (assembly == null)
                return null;

            // Ask that assembly to return the proper Type
            type = assembly.GetType(TypeName);
            if (type != null)
                return type;

        }

        // If we still haven't found the proper type, we can enumerate all of the 
        // loaded assemblies and see if any of them define the type
        //獲取包含調(diào)用當(dāng)前所執(zhí)行代碼的方法的程序集
        var currentAssembly = Assembly.GetExecutingAssembly();
        var referencedAssemblies = currentAssembly.GetReferencedAssemblies();
        foreach (var assemblyName in referencedAssemblies)
        {

            // Load the referenced assembly
            var assembly = Assembly.Load(assemblyName);
            if (assembly != null)
            {
                // See if that assembly defines the named type
                type = assembly.GetType(TypeName);
                if (type != null)
                    return type;
            }
        }

        // The type just couldn't be found...
        return null;

    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,563評論 19 139
  • C、C++、Java?Java Native Interface(JNI)特輯——C反射java函數(shù) 排版不佳建議...
    君華_Joshua閱讀 1,415評論 0 1
  • 兩個現(xiàn)實(shí)中的例子: 1、B超:大家體檢的時候大概都做過B超吧,B超可以透過肚皮探測到你內(nèi)臟的生理情況。這是如何做到...
    幻凌風(fēng)閱讀 4,365評論 0 7
  • 新浪博客,網(wǎng)易博客,搜狐博客,還活著。 微信,QQ,更多的新玩意兒, 自媒體無處不在, 現(xiàn)在有簡書。 吸引我的,是...
    靈山一夫閱讀 274評論 0 0
  • 有多少時候,我們會因?yàn)椤奥犝f……”而左右自己的決定?或者用“聽說……”作為借口來粉飾自己的選擇?還有什么人...
    歡樂V英雄閱讀 573評論 0 4

友情鏈接更多精彩內(nèi)容