zoukankan      html  css  js  c++  java
  • 《C#高级编程》读书笔记(十二):反射

    1,反射

        反射是一个普通术语,它描述了在运行过程中检查和处理程序元素的功能。

    2,System.Type类    

    using System;
    using System.Reflection;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Demo
    {
        public class ClassAnalyzeType
        {
            static StringBuilder OutputText = new StringBuilder();
    
            public static void AnalyzeType(Type t)
            {
                AddToOutput("Type Name:" + t.Name);
                AddToOutput("Full Name:" + t.FullName);
                AddToOutput("Namespace:" + t.Namespace);
    
                Type tBase = t.BaseType;
                if (tBase != null)
                {
                    AddToOutput("Base Type:" + tBase.Name);
                }
    
                Type tUnderlyingSystem = t.UnderlyingSystemType;
                AddToOutput("tUnderlyingSystem:" + tUnderlyingSystem);
                AddToOutput("
    PUBLIC MEMBERS:");
                MemberInfo[] members = t.GetMembers();
                foreach (var memberInfo in members)
                {
                    AddToOutput(memberInfo.DeclaringType + " " + memberInfo.MemberType + " " + memberInfo.Name);
                }
                MessageBox.Show(OutputText.ToString(), "Analysis of type " + t.Name);
            }
    
            static void AddToOutput(string Text)
            {
                OutputText.Append("
    " + Text);
            }
        }
    }

    测试:

    Type t = typeof(int);
    ClassAnalyzeType.AnalyzeType(t);

    输出:

    3,Assembly类

        Assembly类在System.Reflection名称空间中定义,它运行访问给定程序集的元数据,它也包含可以加载和执行程序集的方法。

  • 相关阅读:
    我要AFO啦好伤感啊
    noip2012~2015刷题小记录
    【20161114模拟赛】
    第5模块闯关CSS练习题
    HTML练习题
    Mysql常用命令行大全
    mysql破解密码安装与基本管理
    python 闯关之路四(下)(并发编程与数据库编程) 并发编程重点
    Python/ selectors模块及队列
    python3 中 Event.wait 多线程等待
  • 原文地址:https://www.cnblogs.com/khjian/p/5682663.html
Copyright © 2011-2022 走看看