///
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();
}
}
}
/
//