C# dynamically loads dlls through reflection

///

C# dynamically loads dlls through reflection, reads modules, classes, methods, and features, and implements ioc through reflection + simple factory + configuration files

Calling class:

class Program
{
    static void Main(string[] args)
    {
        SafeInvoke(() =>
        {//Realize general exception handling through delegation
                         #region Creating objects through reflection
            {
                                 //1 Dynamic loading The dll file of the current path is loaded by default, no suffix is ​​required
                Assembly assembly = Assembly.Load("Work.DB.Sqlserver");
                Assembly assembly1 = Assembly.LoadFile(@"E:\winningjob\C#workspace\Projects\MyHomeWork\Work.DB.Sqlserver\bin\Debug\Work.DB.Sqlserver.dll");
                                 Assembly assembly2 = Assembly.LoadFrom("Work.DB.Sqlserver.dll");// It can be the current path or the full path
 
 
                                 //2 Get type (There is more than one way to get type information)
                Type typeDBHelpher = assembly.GetType("Work.DB.Sqlserver.DBHelper");
 
 
                                 //3 Create object 
                object oDBHelper = Activator.CreateInstance(typeDBHelpher);
 
 
                                 //4 Calling methods Instance methods, static methods, overloaded methods Optional: Calling private methods Calling generic methods
                                 {//No parameters
                    MethodInfo method = typeDBHelpher.GetMethod("Show");
                    method.Invoke(oDBHelper, null);
                }
                                 {//There are parameters
                    MethodInfo method = typeDBHelpher.GetMethod("MxdQuery");
                                         method.Invoke(oDBHelper, new object[] {"Test input parameters" });
                }
                                 {//Generic parameters
                    MethodInfo method = typeDBHelpher.GetMethod("ShowGeneric");
                                         method = method.MakeGenericMethod(typeof(string));//If it is a generic method, you need to formulate the type of the parameter first
                                         method.Invoke(oDBHelper, new object[] {"Test input parameters" });
                }
                                 {//When the method to be obtained has overloaded methods, the parameter order and type need to be specified when obtaining the method
                                         MethodInfo method = typeDBHelpher.GetMethod("Show_overload", new Type[] {});
                    method.Invoke(oDBHelper, null);
 
 
                                         MethodInfo method1 = typeDBHelpher.GetMethod("Show_overload", new Type[] {typeof(string) });
                                         method1.Invoke(oDBHelper, new object[] {"Test input parameters" });
                }
            }
            #endregion
            Console.WriteLine("*************************************************");
                         #region Create object reflection + simple factory + configuration file ioc
            {
                  //IDBHelper oDBHelperFactory = SimpleFactory.CreatDBHelper();
                  var oDBHelperFactory = SimpleFactory.CreatDBHelper<IDBHelper>();
                                     oDBHelperFactory.QueryEntity<User>(1);//Here is the test to get a row of data according to Id
                                     oDBHelperFactory.QueryEntityList<Company>();//Test here to get all the data of the model table
            }
            #endregion 
        });         
    }
    /// <summary>
     /// General exception handling
    /// </summary>
     /// <param name="act">corresponding to any logic</param>
    public static void SafeInvoke(Action act)
    {
        try
        {
          act.Invoke();
        }
         catch (Exception ex)//According to the exception type
        {
          Console.WriteLine(ex.Message);
        }
    }  
}

Simple factory class:

class SimpleFactory
{
         //Get the database connection string in the configuration file
    private static string IDBHelperConfig = ConfigurationManager.AppSettings["IDBHelper"];
    private static string dllName = IDBHelperConfig.Split(',')[1];
    private static string className = IDBHelperConfig.Split(',')[0];
 
    public static T  CreatDBHelper<T>()
    {
                 if (true)//Method 1
        {
                         //1 Dynamic loading
            Assembly assembly = Assembly.Load(dllName);
                         //2 Get the type
            Type typeDBHelper = assembly.GetType(className);
                         //3 Create object
            object oDBHelper = Activator.CreateInstance(typeDBHelper);
                         //4 type conversion
            return (T)oDBHelper;
        }
                 else//Method two
        {
            return (T)Activator.CreateInstance(dllName, className).Unwrap();
        }
 
    }
}

/

//

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值