zoukankan      html  css  js  c++  java
  • .Net 5 C# 反射(Reflection)

    什么是反射

    官方定义:

    反射提供描述程序集、模块和类型的对象(Type 类型)。 可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型,然后调用其方法或访问器字段和属性。 如果代码中使用了特性,可以利用反射来访问它们。

    个人理解:

    当我们需要获取一个程序集或者一个类的所有属性(名称、类型等)、方法(名称、类型等)等信息时,我们就需要反射。

    反射就像一个朦胧的镜子,抽象折射一个程序集或者一个类的所有信息。
    反射就像一个朦胧的镜子,抽象折射一个程序集或者一个类的所有信息。
    反射就像一个朦胧的镜子,抽象折射一个程序集或者一个类的所有信息。
    重复3次,希望你能咀嚼

    有什么用?怎么用?

    比如我们定义一个int类型的i变量,想获取它的类型信息,我们就可以使用反射

    获取类型的类型信息。

    // Using GetType to obtain type information:
    int i = 42;
    Type type = i.GetType();
    Console.WriteLine(type);
    

    输出为:System.Int32。

    或者你会困惑,我不是已经知道了int类型了嘛?还要反射干哈?

    这里只是举例,那如果是泛型呢?

    获取泛型信息

    // The following method displays information about a generic
        // type.
        private static void DisplayGenericType(Type t)
        {
            Console.WriteLine("
     {0}", t);
            Console.WriteLine("   是否是泛型? {0}",
                t.IsGenericType);
          
        }
    

    如果是加载的程序集呢?

    获取程序集的信息

    // Using Reflection to get information of an Assembly:
    Assembly info = typeof(int).Assembly;
    Console.WriteLine(info);
    

    输出为:System.Private.CoreLib, Version=4.0.0.0, Culture=neutral,PublicKeyToken=7cec85d7bea7798e。

    从已加载的程序集获取 Type 对象

    当我们需要获取程序集内的exe的全名时

    // Loads an assembly using its file name.
    Assembly a = Assembly.LoadFrom("MyExe.exe");
    // Gets the type names from the assembly.
    Type[] types2 = a.GetTypes();
    foreach (Type t in types2)
    {
        Console.WriteLine(t.FullName);
    }
    

    查看类的信息

    下列示例演示如何列出类的构造函数,在本例中即指 String 类。

    //请注意引用反射命名空间
    using System;
    using System.Reflection;
    
    class ListMembers
    {
        public static void Main()
        {
            Type t = typeof(System.String);
            Console.WriteLine("Listing all the public constructors of the {0} type", t);
            //BindingFlags是Reflection空间下的枚举类型
            // 使用GetConstructors方法获取Public(公共),Instance(实例)的构造方法
            ConstructorInfo[] ci = t.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
            Console.WriteLine("//Constructors");
            PrintMembers(ci);
        }
    
        public static void PrintMembers(MemberInfo[] ms)
        {
            foreach (MemberInfo m in ms)
            {
                Console.WriteLine("{0}{1}", "     ", m);
            }
            Console.WriteLine();
        }
    }
    

    首尾呼应、重复强调、重要事情说三遍

    当我们需要获取一个程序集或者一个类的所有属性(名称、类型等)、方法(名称、类型等)等信息时,我们就需要反射。

    反射就像一个朦胧的镜子,抽象折射一个程序集或者一个类的所有信息。
    反射就像一个朦胧的镜子,抽象折射一个程序集或者一个类的所有信息。
    反射就像一个朦胧的镜子,抽象折射一个程序集或者一个类的所有信息。

    更详细的信息参考官方文档

    后记

    本人不是大佬,只是道路先行者,在落河后,向后来的人大喊一声,这里有坑,不要过来啊!

    纵然如此,依旧有人重复着落河,重复着呐喊······

    个人博客网站 Blog

    文章后续会在公众号更新,微信搜索 OneByOneDotNet 即可关注。

    你的一分鼓励,我的十分动力,点赞免费,感恩回馈。喜欢就点赞评论吧,双击6666~

  • 相关阅读:
    调用控制台程序函数 RunProcess
    url传递中文的解决方案(备忘)
    Microsoft.SharePoint.SPException 安全性验证无效——错误解决
    Windows SharePoint Services 虚拟服务器没被配置为与 ASP.NET 2.0.50727.42 一起使用解决办法
    automation服务器不能创建对象
    InfoPath窗体事件列表说明和示例使用
    ORA01113:文件n需要介质恢复 (转载)
    DevExpress 第三方控件汉化的全部代码和使用方法 (转载)
    爱的感觉(转载)
    关于Oracle 01122,01110,01207的错误和解决(转载)
  • 原文地址:https://www.cnblogs.com/ma-nong01/p/14323406.html
Copyright © 2011-2022 走看看