schematic diagram

/// <summary> /// 1 dll-IL-metadata- reflex /// 2 Reflection loading dll, read module, class , method /// 3
Reflection creates objects , reflex + Simple factory + configuration file , Damage case Creating generics /// Reflection reflects the happiness of programmers /// Reflection is everywhere ,MVC AOP IOC ORM
Attribute /// IL: Interlanguage , Standard object oriented language , But it's not very easy to read /// metadata: metadata , Data list , It just describes what's in the class ///
reflex :Reflection, System.Reflection Namespace , Is a help class library provided by Microsoft /// </summary> class Program {
static void Main(string[] args) { try { #region Basic use of reflection {
//-------------------------- 1, Dynamic loading ( Gets the data for the assembly )-------------------------- Assembly
assembly= Assembly.Load("DB.SqlServer");// dll name Search in the current directory by default Assembly assembly1
= Assembly.LoadFile(@"D:\MyReflection\bin\Debug\DB.SqlServer.dll");// Full name =
Full path +dll name + suffix Assembly assembly2 = Assembly.LoadFrom("DB.SqlServer.dll");// Full name
Assembly assembly3 = Assembly.LoadFrom(
@"D:\MyReflection\bin\Debug\DB.SqlServer.dll");// Full name foreach (Type type in
assembly.GetTypes()) { Console.WriteLine(type.Name); foreach (MethodInfo method
in type.GetMethods()) { Console.WriteLine(method.Name); } } } {
//------------------------1, Dynamic loading ( Creating instances and using assembly data )-----------------------------
Assembly assembly = Assembly.Load("DB.SqlServer");// dll name Search in the current directory by default //2, Get type
Type type = assembly.GetType("DB.SqlServer.SqlServerHelper"); //3, create object object
oDbHelper= Activator.CreateInstance(type); //oDbHelper.Query();// The compiler doesn't recognize it c#
It's a strongly typed language , No Query //dynamic dDbHelper = Activator.CreateInstance(type);
//dDbHelper.Query(); //dynamic Is a dynamic type , You can avoid compiler checks , Runtime check There are security issues //4, Type conversion
IDBHelper iDBHelper = oDbHelper as IDBHelper; //5, Call method iDBHelper.Query(); } {
//--------------------------- Create instance with factory --------------------------- IDBHelper
iDBHelper= SimlpFactory.CreateInstentce(); iDBHelper.Query(); } {
//------------------------- Reflection selects different constructors to create objects ---------------------- Assembly
assembly= Assembly.Load("Ruanmou.DB.SqlServer"); Type type = assembly.GetType(
"Ruanmou.DB.SqlServer.ReflectionTest"); object oDbHelper = Activator.
CreateInstance(type); object oDbHelper1 = Activator.CreateInstance(type, new
object[] { " jack " }); object oDbHelper2 = Activator.CreateInstance(type, new
object[] { " Who am I? " }); object oDbHelper3 = Activator.CreateInstance(type, new
object[] { 123 }); } {
//-------------------- Reflection needs to get methods first , Then the method Invoke-------------------- Assembly assembly
= Assembly.Load("DB.SqlServer"); Type type = assembly.GetType(
"DB.SqlServer.ReflectionTest"); object oTest = Activator.CreateInstance(type);
MethodInfo show1 = type.GetMethod("Show1"); show1.Invoke(oTest, new object[0]);
// Reflection call method MethodInfo show2 = type.GetMethod("Show2"); show2.Invoke(oTest, new
object[] { 123 });// Reflection call method , When parameters are required , The parameter type must be the same as the method parameter type // Overloading methods : MethodInfo show3
= type.GetMethod("Show3", new Type[] { typeof(string), typeof(int) }); show3.
Invoke(oTest, new object[] { " Feige ", 134 });// Reflection call method MethodInfo show4 = type.
GetMethod("Show4", BindingFlags.NonPublic | BindingFlags.CreateInstance); show4.
Invoke(oTest, new object[] { "Tenk" });// Reflection call method MethodInfo show5 = type.
GetMethod("Show5"); show5.Invoke(oTest, new object[] { " Peng Yuyan " });// Reflection calls static methods :
Object Can be passed in , It can also be passed in null show5.Invoke(null, new object[] { "Korey" }); } {
//--------------- Reflection call generic correlation ------------------------------ // Generation of booths after generic compilation `1 `2 `3
Assembly assembly = Assembly.Load("DB.SqlServer"); Type type = assembly.GetType(
"DB.SqlServer.GenericMethod"); object oTest = Activator.CreateInstance(type);
MethodInfo genericMethod = type.GetMethod("Show"); MethodInfo genericMethod1 =
genericMethod.MakeGenericMethod(new Type[] { typeof(int), typeof(string), typeof
(DateTime) });// Of a generic method Specific parameters genericMethod1.Invoke(oTest, new object[] { 123,
"jun", DateTime.Now }); // The passed in parameters must be the same as declared // In a generic class Calling generic methods Assembly assembly =
Assembly.Load("DB.SqlServer"); Type type = assembly.GetType(
"DB.SqlServer.GenericClass`3"); // Gets a generic type // GenericClass<int,string,DateTime>
genericClass = new GenericClass<int, string, DateTime>() Type type1 = type.
MakeGenericType(new Type[] { typeof(int), typeof(string), typeof(DateTime) });
object oGenericTest = Activator.CreateInstance(type1); MethodInfo genericMethod
= type1.GetMethod("Show"); genericMethod.Invoke(oGenericTest, new object[] { 123
, " zero zero ", DateTime.Now }); } { //----------------- Reflection damage simple interest
It's actually reflection calling private constructors ------------------- Singleton singleton = new Singleton();
Singleton singleton1 = Singleton.GetInstance(); Singleton singleton2 = Singleton
.GetInstance(); Singleton singleton3 = Singleton.GetInstance(); Singleton
singleton4= Singleton.GetInstance(); Console.WriteLine(singleton1.Equals(
singleton4)); Assembly assembly = Assembly.Load("DB.SqlServer"); Type type =
assembly.GetType("DB.SqlServer.Singleton"); object oSingleton1 = Activator.
CreateInstance(type, true); // Completely equivalent to New Singleton object oSingleton2 = Activator
.CreateInstance(type, true); object oSingleton3 = Activator.CreateInstance(type,
true); object oSingleton4 = Activator.CreateInstance(type, true); Console.
WriteLine(oSingleton1.Equals(oSingleton4)); } {
//----------------------- Reflection uses property fields ---------------- People people = new People();
people.Id = 123; people.Name = " zero zero "; people.Description = " La La La "; Console.
WriteLine($"people.Id={people.Id}"); Console.WriteLine($
"people.Name={people.Name}"); Console.WriteLine($
"people.Description={people.Description}"); // 1,GetValue Actually, it makes sense // 2,SetValue
It doesn't seem to work Type type = typeof(People); object oPeople = Activator.CreateInstance(type
); foreach (PropertyInfo prop in type.GetProperties()) { if (prop.Name.Equals(
"Id")) { prop.SetValue(oPeople, 123); } else if (prop.Name.Equals("Name")) {
prop.SetValue(oPeople, " zero zero "); } Console.WriteLine($
"oPeople.{prop.Name}={prop.GetValue(oPeople)}"); } foreach (FieldInfo field in
type.GetFields()) { if (field.Name.Equals("Description")) { field.SetValue(
oPeople, " La La La "); } } } #endregion } catch (Exception ex) { Console.WriteLine(ex.
Message); } Console.Read(); } } public class SimlpFactory { private static
string IDBHelperConfig = ConfigurationManager.AppSettings["IDBHelperConfig"];
private static string DllName = IDBHelperConfig.Split(',')[1]; private static
string TypeName = IDBHelperConfig.Split(',')[0]; public static IDBHelper
CreateInstentce() { Assembly assembly = Assembly.Load(DllName); Type type =
assembly.GetType(TypeName); object oDbHelper = Activator.CreateInstance(type);
return oDbHelper as IDBHelper; } }

Technology
©2019-2020 Toolsou All rights reserved,
vue Changes in data Interface not updated Python realization js And Django Front and back interaction in the background JavaSwing To achieve a simple Lianliankan games C# Making a simplified version of calculator elementui Shuttle box el-transfer Display list content text too long JVM summary Regression of dependent variable order categories (R language )【C#】 The realization of student achievement information management system use css Design a simple style html login interface C language --------- Tetris ( source code )