• 使用反射实现延迟绑定


    反射允许我们在编译期或运行时获取程序集的元数据,通过反射可以做到:
    ● 创建类型的实例
    ● 触发方法
    ● 获取属性、字段信息
    ● 延迟绑定
    ......


    如果在编译期使用反射,可通过如下2种方式获取程序集Type类型:
    1、Type类的静态方法
    Type type = Type.GetType("somenamespace.someclass");

    2、通过typeof
    Type type = typeof(someclass);

    如果在运行时使用反射,通过运行时的Assembly实例方法获取Type类型:

    Type type = asm.GetType("somenamespace.someclass");

      获取反射信息

    有这样的一个类:

    public class Student
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public float Score { get; set; }
    
            public Student()
            {
                this.Id = -1;
                this.Name = string.Empty;
                this.Score = 0;
            }
    
            public Student(int id, string name, float score)
            {
                this.Id = id;
                this.Name = name;
                this.Score = score;
            }
    
            public string DisplayName(string name) 
            {
                return string.Format("学生姓名:{0}", name);
            }
    
            public void ShowScore()
            {
                Console.WriteLine("学生分数是:" + this.Score);
            }
        }

    通过如下获取反射信息:

    static void Main(string[] args)
            {
                Type type = Type.GetType("ConsoleApplication1.Student");
                //Type type = typeof (Student);
    
                Console.WriteLine(type.FullName);
                Console.WriteLine(type.Namespace);
                Console.WriteLine(type.Name);
    
                //获取属性
                PropertyInfo[] props = type.GetProperties();
                foreach (PropertyInfo prop in props)
                {
                    Console.WriteLine(prop.Name);
                }
    
                //获取方法
                MethodInfo[] methods = type.GetMethods();
                foreach (MethodInfo method in methods)
                {
                    Console.WriteLine(method.ReturnType.Name);
                    Console.WriteLine(method.Name);
                }
                Console.ReadKey();
            }

      延迟绑定


    在通常情况下,为对象实例赋值是发生在编译期,如下:
    Student stu = new Student();
    stu.Name = "somename";

    而"延迟绑定",为对象实例赋值或调用其方法是发生在运行时,需要获取在运行时的程序集、Type类型、方法、属性等。

    //获取运行时的程序集
                Assembly asm = Assembly.GetExecutingAssembly();
    
                //获取运行时的Type类型
                Type type = asm.GetType("ConsoleApplication1.Student");
    
                //获取运行时的对象实例
                object stu = Activator.CreateInstance(type);
    
                //获取运行时指定方法
                MethodInfo method = type.GetMethod("DisplayName");
                object[] parameters = new object[1];
                parameters[0] = "Darren";
    
                //触发运行时的方法
                string result = (string)method.Invoke(stu, parameters);
                Console.WriteLine(result);
                Console.ReadKey();
  • 相关阅读:
    Python lambda函数
    python 获取日期
    <base>元素
    django--开发博客
    django修改时区,数据库
    django初探
    python创建虚拟环境
    资源记录页面
    组管理
    远程管理命令
  • 原文地址:https://www.cnblogs.com/darrenji/p/3817999.html
Copyright © 2020-2023  润新知