zoukankan      html  css  js  c++  java
  • 反射定义及基础案例

    1.什么是元数据(MetaData)和反射(reflection)

    一般情况下我们的程序都在处理数据的读、写、操作和展示。但是有些程序操作的数据不是数字、文本、图片,而是程序和程序类型本身的信息。

    (1)元数据是包含程序以及类型信息的数据,它保存在程序的程序集当中。

    (2)程序在运行的时候,可以查看其它程序集或者其它本身的元数据,这个行为就是反射。

    2.Type类

    BCL声明了一个Type类型(它是抽象类),用来包含类型的特性,使用这个类型的对象能让我们获取程序使用的类型的信息

    --由于Type是抽象类,不能被实例化,而是在运行时,CLR创建从Type(RuntimeType)派生的类型的实例。当我们要访问这些实例的时候,CLR不会返回派生类的引用,而是返回Type基类的引用

     3.如何获取一个Type类对象

    方式一:通过GetType方法

    using System;
    using System.Reflection;
    
    namespace DeldgateTest
    {
        public class Program
        {
            public delegate int MyDeletage(int s);
            public static void Main(string[] args)
            {
                var bc = new BaseClass();
                var dc = new DerivedClass();
                BaseClass[] arr = new BaseClass[] {bc,dc };
                foreach (var item in arr)
                {
                    Type t = item.GetType();
                    Console.WriteLine("Object Type:{0}", t.Name);
                    FieldInfo[] fi = t.GetFields();
                    foreach (var f in fi)
                    {
                        Console.WriteLine("  Field:{0}",f.Name);
                    }
                    Console.WriteLine();
                }
                Console.WriteLine("End!");
                Console.ReadKey();
            }
        }
    
        public class BaseClass
        {
            public int BaseField = 0;
        }
    
        public class DerivedClass : BaseClass
        {
            public int DerivedField = 0;
        }
    }
    View Code

    运行结果:

    方式二: 通过typeof()方法获取一个类型的Type对象引用

    using System;
    using System.Reflection;
    
    namespace DeldgateTest
    {
        public class Program
        {
            public delegate int MyDeletage(int s);
            public static void Main(string[] args)
            {
                var bc = new BaseClass();
                var dc = new DerivedClass();
                Type t = typeof(DerivedClass);
                Console.WriteLine("Object Type:{0}", t.Name);
                FieldInfo[] fi = t.GetFields();
                foreach (var f in fi)
                {
                    Console.WriteLine("Object Field:{0}", f.Name);
                }
                Console.WriteLine("End!");
                Console.ReadKey();
            }
        }
    
        public class BaseClass
        {
            public int BaseField = 0;
        }
    
        public class DerivedClass : BaseClass
        {
            public int DerivedField = 0;
        }
    }
    View Code

    运行结果:

    4.通过反射获取对象字段名称及字段值

    using System;
    using System.Reflection;
    
    namespace DeldgateTest
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                UserInfo user = new UserInfo();
                user.ID = 1;
                user.age = 18;
                user.Name = "小明";
                Type t = user.GetType();
                Console.WriteLine("Object Type:{0}", t.Name);
                foreach (System.Reflection.PropertyInfo p in user.GetType().GetProperties())
                {
                    Console.WriteLine("  Name:{0}; Value:{1}", p.Name,p.GetValue(user));
                }
                Console.WriteLine();
                Console.WriteLine("End!");
                Console.ReadKey();
            }
        }
    
        public class UserInfo
        {
            public int ID { get; set; }
    
            public int age { get; set; }
    
            public string Name { get; set; }
        }
    }
    View Code

    运行结果:

    5.获取基类型:BasetType

    using System;
    using System.Reflection;
    
    namespace DeldgateTest
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                var base1 = typeof(System.String).BaseType;
                var base2 = typeof(System.IO.FileStream).BaseType;
                var base3 = typeof(UserInfo).BaseType;
                Console.WriteLine("base1 Type:{0}", base1);
                Console.WriteLine("base2 Type:{0}", base2);
                Console.WriteLine("base3 Type:{0}", base3);
                Console.WriteLine();
                Console.WriteLine("End!");
                Console.ReadKey();
            }
        }
    
        public class Anmial
        {
            public int length { get; set; }
        }
    
        public class People:Anmial
        {
            public string color { get; set; }
        }
        public class UserInfo: People
        {
            public int ID { get; set; }
    
            public int age { get; set; }
    
            public string Name { get; set; }
        }
    }
    View Code

     运行结果:

    (看下图得知,获取的是当前实例的上一级类型)

    6.获取接口类型

    using System;
    using System.Reflection;
    
    namespace DeldgateTest
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                foreach (var item in typeof(UserInfo).GetInterfaces())
                {
                    Console.WriteLine("Name Type:{0}", item.Name);
                }
                
                Console.WriteLine();
                Console.WriteLine("End!");
                Console.ReadKey();
            }
        }
    
        public interface TUser
        {
            int ID { get; set; }
    
            int age { get; set; }
    
            string Name { get; set; }
        }
        public interface TPeople {
            string color { get; set; }
        }
        public class UserInfo: TUser,TPeople
        {
            public int ID { get; set; }
    
            public int age { get; set; }
    
            public string Name { get; set; }
    
            public string color { get; set; }
    
        }
    }
    View Code

    运行结果:GetInterfaces()

  • 相关阅读:
    第九周个人总结
    用户模板和用户场景
    windows 滚动截图单文件版
    vue一键复制实现 笔记
    django 配置mysql流程以及运行报错的解决
    django urls导入views报错
    python学习2
    python学习1
    spark学习第五天
    spark第四天
  • 原文地址:https://www.cnblogs.com/yxcn/p/13026516.html
Copyright © 2011-2022 走看看