• 當前位置:首頁 > IT技術 > Windows編程 > 正文

    c#代碼的執行過程及反射
    2021-10-18 17:42:29


    0.c#代碼的執行過程:

    c#(編譯器)-->dll/exe(metadata/IL)-->CRL-->機器碼


    1. appsettings.json配置文件配置要訪問的類和dll

    "ReflictionConfig": "Zhaoxi.AspNetCore.DB.MySql.MySqlHelper,Zhaoxi.AspNetCore.DB.MySql.dll"

    2.代碼讀取dll,實例化一個配置類的對象

    public static IDBHelper CreateInstance()

    {

    string ReflictionConfig = CustomConfigManager.GetConfig("ReflictionConfig");

    string tyepName= ReflictionConfig.Split(",")[0];

    string dllName = ReflictionConfig.Split(",")[1];

    //Assembly assembly = Assembly.Load(dllName); //Dll名稱,不需要后綴

    Assembly assembly3 = Assembly.LoadFrom(dllName); //dll名稱(需要后綴)


    Type type = assembly3.GetType(tyepName);

    object obj = Activator.CreateInstance(type);

    return obj as IDBHelper;

    }

    3. 反射可以突破方法的權限限制

    100_000_000

    4. 反射性能并不會消耗多大,100萬次循環和普通方法相差3倍的時間,

    單例模式=是一種常見類的寫法,

    public class SingletonA

    {

    //私有成員,使用時分配內存

    private static SingletonA _instance = null;

    //私有構造,杜絕直接new類

    private SingletonA() { }

    //獲取實例

    public static SingletonA GetInstance ()

    {

    if (_instance == null)

    {

    _instance = new SingletonA();

    }

    return _instance;

    }

    }


    本文摘自 :https://blog.51cto.com/u

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码