zoukankan      html  css  js  c++  java
  • 反射常规

    上端调用下端的实例

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Configuration;
     4 using System.Linq;
     5 using System.Reflection;
     6 using System.Text;
     7 
     8 namespace 反射
     9 {
    10     class Program
    11     {
    12         static void Main(string[] args)
    13         {
    14            
    15             #region 多构造函数 破坏单例,创建泛型
    16             //创建多构造函数
    17             {
    18                 string typeObj = ConfigurationManager.AppSettings["data"];
    19 
    20                 Type type = Assembly.Load("反射").GetType(typeObj);
    21                 
    22                 object stu1 = Activator.CreateInstance(type);
    23 
    24                 //type后面的参数表示该反射类型的构造函数的参数,参数必须一一对应写入才能找到相应的构造函数
    25                 object stu2 = Activator.CreateInstance(type, 857);
    26                 object stu3 = Activator.CreateInstance(type, "张三");
    27                 object stu4 = Activator.CreateInstance(type, 123, "李四");
    28             }
    29 
    30             //反射黑科技,破坏单例
    31             {
    32                 string stuObj= ConfigurationManager.AppSettings["singleton"];
    33                 Type type = Assembly.Load("反射").GetType(stuObj);
    34 
    35                 //反射可以让单例创建多次,增加true即可
    36                 object obj1 = Activator.CreateInstance(type, true);
    37                 object obj2 = Activator.CreateInstance(type, true);
    38                 object obj3 = Activator.CreateInstance(type, true);
    39                 object obj4 = Activator.CreateInstance(type, true);
    40             }
    41 
    42             //泛型类反射
    43             {
    44                 string typeObj = ConfigurationManager.AppSettings["fanxing"];
    45                 Type type = Assembly.Load("反射").GetType(typeObj).MakeGenericType(typeof(int),typeof(string),typeof(bool));
    46                 object obj = Activator.CreateInstance(type);
    47             }
    48             Console.ReadLine();
    49             #endregion
    50 
    51             #region  发射调用实例方法,静态方法,重载方法
    52             {
    53                 string stuObj = ConfigurationManager.AppSettings["data"];
    54                 Type type = Assembly.Load("反射").GetType(stuObj);
    55                 object obj = Activator.CreateInstance(type);
    56                 foreach (var item in type.GetMethods())
    57                 {
    58                     Console.WriteLine(item.Name);
    59                 }
    60                 {
    61                     MethodInfo method = type.GetMethod("Study"); //调用Student对象中的Study()方法
    62                     method.Invoke(obj, new object[] {"大爷"}); //开始调用,obj为该类型对象,后面为Study()方法的参数
    63                 }
    64                 {
    65                     //重载
    66                     MethodInfo method = type.GetMethod("Show"); 
    67                     method.Invoke(obj,null);
    68                 }
    69                 {
    70                     //重载2
    71                     MethodInfo method = type.GetMethod("Show",new Type[] { typeof(string)}); //new Type[] { typeof(string),typeof(...),...} 表示参数类型
    72                     method.Invoke(obj,new object[] {"小兵"}); //添加实际参数
    73                 }
    74 
    75             }
    76             #endregion
    77 
    78             /*
    79             <appSettings>
    80                <add key="data" value="反射.Student"/>
    81                <add key="singleton" value="反射.StuSingleton"/>
    82                <add key="fanxing" value="反射.People`3"/> //注意这里People类中有三个泛型,要用占位符 `3 来表示
    83             </appSettings>
    84            */
    85         }
    86     } 
    87 }


    下面是几个类的实例

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace 反射
     7 {
     8     class People<T, U, X>
     9     {
    10 
    11     }
    12 }
    泛型类
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace 反射
     7 {
     8     class Student
     9     {
    10         private int id;
    11         private string name;
    12         public Student()
    13         {
    14 
    15         }
    16         public Student(int id)
    17         {
    18             this.id = id;
    19         }
    20         public Student(string name)
    21         {
    22             this.name = name;
    23         }
    24         public Student(int id,string name)
    25         {
    26             this.id = id;
    27             this.name = name;
    28         }
    29         public void Show()
    30         {
    31             Console.WriteLine("这是一个学生");
    32         }
    33         
    34         public void Show(string name)
    35         {
    36             this.name = name;
    37             Console.WriteLine("这个学生是{0}",name);
    38         }
    39         public static void Study()
    40         {
    41             Console.WriteLine("这是一个爱学习的学生");
    42         }
    43     }
    44 }
    普通类
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace 反射
     7 {
     8     class StuSingleton
     9     {
    10         private static StuSingleton _instance = null;
    11         private StuSingleton()
    12         {
    13             Console.WriteLine("创建单例模式");
    14         }
    15         static StuSingleton()
    16         {
    17             _instance = new StuSingleton();
    18         }
    19         public static StuSingleton Instance
    20         {
    21             get
    22             {
    23                 return _instance;
    24             }
    25         }
    26     }
    27 }
    单例类
    世界上最可怕事情是比我们优秀的人比我们还努力
  • 相关阅读:
    long和Long的区别
    C语言的变量的内存分配
    Java蓝桥杯 算法提高 九宫格
    Java实现 蓝桥杯算法提高金明的预算方案
    Java实现 蓝桥杯 算法提高 新建Microsoft world文档
    Java实现 蓝桥杯 算法提高 快乐司机
    Java实现 蓝桥杯 算法提高 三角形
    Java实现 蓝桥杯 算法提高 三角形
    Java实现 蓝桥杯 算法提高 三角形
    Java实现 蓝桥杯 算法提高 三角形
  • 原文地址:https://www.cnblogs.com/AlexOneBlogs/p/7387534.html
Copyright © 2011-2022 走看看