Schematic diagram

/// <summary>
    ///1 the DLL - IL - metadata - reflection
    ///2 reflection load DLL, read module, class, method
    ///3 reflection create object, reflection + simple factory + configuration file, break singleton create generics
    ///Reflection reflects the programmer's happiness
    ///Reflection is everywhere, MVC, AOP, IOC, ORM Attribute
    ///IL: Intermediate language, standard object-oriented language, but not very easy to read
    ///Metadata: Metadata, a list of data, just describes what's in a class
    ///Reflection: System.Reflection namespace is a helper class library provided by Microsoft
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                #regionBasic use of reflection
                {
                    / / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1, dynamic load (for the assembly of data) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
                    Assembly assembly = Assembly.Load("DB.SqlServer");// DLL names are found 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 intype.GetMethods()) { Console.WriteLine(method.Name); }}} {/ / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1, dynamic load (using the assembly data instance is created and used) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
                    Assembly assembly = Assembly.Load("DB.SqlServer");// DLL names are found in the current directory by default
                    //2
                    Type type = assembly.GetType("DB.SqlServer.SqlServerHelper");
                    // create an object
                    object oDbHelper = Activator.CreateInstance(type);
                    //oDbHelper.Query(); // the compiler does not recognize c# as strongly typed and cannot Query
                    //dynamic dDbHelper = Activator.CreateInstance(type);
                    //dDbHelper.Query(); // Dynamic is a dynamic type that can avoid compiler checks and run time checks have security issues
                    // type conversion
                    IDBHelper iDBHelper = oDbHelper as IDBHelper;
                    //5
                    iDBHelper.Query();
                }
                {   / / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- instance is created with factory -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
                    IDBHelper iDBHelper = SimlpFactory.CreateInstentce();
                    iDBHelper.Query();
                } 
                {
                    / / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- reflection to select 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 method need to get first, then the method to 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 calls the method
                    MethodInfo show2 = type.GetMethod("Show2");
                    show2.Invoke(oTest, new object[] { 123 });// Reflection calls a method. When parameters are required, the parameter type must be the same as the method parameter type
                    // Override method:
                    MethodInfo show3 = type.GetMethod("Show3".new Type[] { typeof(string), typeof(int)}); show3.Invoke(oTest,new object[] { "Flying elder brother".134 });// reflection calls the method

                    MethodInfo show4 = type.GetMethod("Show4", BindingFlags.NonPublic | BindingFlags.CreateInstance);
                    show4.Invoke(oTest, new object[] { "Tenk" });// reflection calls the method

                    MethodInfo show5 = type.GetMethod("Show5");
                    show5.Invoke(oTest, new object[] { "Eddie Peng" });// Reflection calls static methods: an instance of an object can be passed in or null
                    show5.Invoke(null.new object[] { "Korey" });
                }
                {
                    / / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- reflection calls generic related -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
                    // Generics are compiled to generate stand character '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) });// Specify the parameters of the generic method
                    genericMethod1.Invoke(oTest, new object[] { 123."jun", DateTime.Now }); // The parameters passed in must be the same as declared
                    // Call generic methods in a generic class
                    Assembly assembly = Assembly.Load("DB.SqlServer");
                    Type type = assembly.GetType("DB.SqlServer.GenericClass`3"); // Get 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 simple interest Is actually reflection calls private constructor -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
                    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); // Exactly 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 the property field ----------------
                        People people = new People();
                        people.Id = 123;
                        people.Name = "Zero zero.";
                        people.Description = "La la la la";
                        Console.WriteLine($"people.Id={people.Id}");
                        Console.WriteLine($"people.Name={people.Name}");
                        Console.WriteLine($"people.Description={people.Description}");
                        // 1, GetValue is meaningful
                        // SetValue ()
                        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 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 asIDBHelper; }}Copy the code