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名称空间中定义,它运行访问给定程序集的元数据,它也包含可以加载和执行程序集的方法。