1. How to use
Find DLL files,
Operation of dll files through various methods in the Reflection Reflection library
2. Steps
Loading DLL file
Assembly assembly1 = ("SqlServerDB");//Method 1: This DLL file should be under the startup projectstring filePath = + ""; Assembly assembly2 = (filePath + @"\");//Method 2: Complete pathAssembly assembly3 = (filePath + @"\");//Method 3: Complete pathAssembly assembly4 = (@"");//Method Three:Complete path
Get the specified type
foreach (var item in ())// Find all types, which means how many classes are there{ (); }
Get the constructor
Type type = ("");//Call in ReflectionTest classforeach (var ctor in ( | | )) { ($"Construction method:{}"); foreach (var param in ()) { ($"Construction method的参数:{}"); } } //【3】Instantiation//ReflectionTest reflectionTest = new ReflectionTest();//This instantiation is to know the specific type--static //object objTest = (type);//Dynamic instantiation--call our constructorobject objTest1 = (type, new object[] { "string" });//Dynamic instantiation--Calling our parameter constructor method //Calling the private constructor//ReflectionTest reflectionTest = new ReflectionTest(); //Ordinary callobject objTest2 = (type, true);
Calling non-constructor methods
object objTest2 = (type, true); //Calling normal methodsReflectionTest reflectionTest = objTest2 as ReflectionTest;//The advantage of as conversion, it does not report an error, and if the type is incorrect, it will return nullreflectionTest.Show1(); //Calling private methodvar method = ("Show2", | ); (objTest2, new object[] { });
Calling generic methods
//Generics have no parametersvar method3 = ("Show3");//Find the specified methodvar genericMethod = (new Type[] { typeof(int) });//Specify the generic parameter type T(objTest2, new object[] { }); //Generics have parametersvar method4 = ("Show4");//Find the specified methodvar genericMethod4 = (new Type[] { typeof(string) });//Specify the generic parameter type T(objTest2, new object[] { 123, "Generic string parameters" });
Reflection test class
In the file located in
/// <summary> /// Reflection test class /// </summary> public class ReflectionTest { //Private constructor private ReflectionTest() { ("This is a private parameter-free constructor"); } //Ordinary constructor //public ReflectionTest() //{ // ("This is a parameterless constructor"); //} public ReflectionTest(string name) { ($"This is a parameter constructor+The parameter value is:{name}"); } public void Show1() { ("Call normal methods", ()); } private void Show2() { ("Call private methods",()); } public void Show3<T>() { ("Call parameterless generic method", ()); } public void Show4<T>(int id,string name) { ($"Calling parameter generic methods,The parameters are{id},{name}", ()); } }
Manage generic classes and generic methods
Loading DLL file
Assembly assembly = (@"");
Get the specified type
Type type = ("`2").MakeGenericType(typeof(int), typeof(string));//Specific types of parameters must be given
Calling generic methods
object objTest2 = (type); var method = ("GenericMethod").MakeGenericMethod(typeof(int)); (objTest2, new object[] { });
Reflection test class
In the file located in
public class GenericClass<T,W> { public void GenericMethod<TType>() { ("Generic class call + generic method"); } }
Operation class attribute field
Loading DLL file
Assembly assembly2 = ("");
Get the specified type
Type type2 = ("");
Calling generic methods
object obj = (type2); foreach (var property in ()) { (); //Set values for attributes if (("Id")) { (obj, 1); } else if (("Name")) { (obj, "Learn Programming"); } else if (("Phone")) { (obj, "123459789"); } //Get the attribute value ((obj)); }
Reflection test class
In the file located in
public class PropertyClass { public int Id { get; set; } public string Name { get; set; } public string Phone { get; set; } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.