⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reflector.cs

📁 Microsoft.NET.框架程序设计修订版中的书中源码
💻 CS
字号:
/******************************************************************************
Module:  Reflector.cs
Notices: Copyright (c) 2002 Jeffrey Richter
******************************************************************************/


// Define either V1, V2, or V3 to match the example from the book
#define V3


///////////////////////////////////////////////////////////////////////////////


using System;
using System.Reflection;


///////////////////////////////////////////////////////////////////////////////


class App {
   static void Main() {
#if V1 || V3
      Assembly assem = Assembly.GetExecutingAssembly();
      Reflector.ReflectOnAssembly(assem);
#endif
#if V2
      foreach (Assembly assem in 
         AppDomain.CurrentDomain.GetAssemblies()) {

         Reflector.ReflectOnAssembly(assem);
      }
#endif

      Console.WriteLine("Press <Enter> to exit.");
      Console.ReadLine();
   }
}


///////////////////////////////////////////////////////////////////////////////


public class Reflector {
   public static void ReflectOnAssembly(Assembly assem) {
      WriteLine(0, "Assembly: {0}", assem);

      // Find Modules
      foreach (Module m in assem.GetModules()) {
         WriteLine(1, "Module: {0}", m);

         // Find Types
         foreach (Type t in m.GetTypes()) {
            WriteLine(2, "Type: {0}", t);

            // Find Members
#if V1 || V2
            foreach (MemberInfo mi in t.GetMembers())
               WriteLine(3, "{0}: {1}", mi.MemberType, mi);
#endif
#if V3
            BindingFlags bf = BindingFlags.DeclaredOnly | 
               BindingFlags.NonPublic | BindingFlags.Public | 
               BindingFlags.Instance  | BindingFlags.Static;

            foreach (MemberInfo mi in t.GetMembers(bf))
               WriteLine(3, "{0}: {1}", mi.MemberType, mi);
#endif
         }
      }
   }

   private static void WriteLine(Int32 indent, String format, 
      params Object[] args) {

      Console.WriteLine(new String(' ', 3 * indent) + format, args);
   }
}


///////////////////////////////////////////////////////////////////////////////


class SomeType {
   public class InnerType {}
   public Int32 SomeField = 0;
   private static String goo = null;

   private void SomeMethod() { } 

   private TimeSpan SomeProperty {
      get { return new TimeSpan(); }
      set { } 
   }
   
   public static event System.Threading.ThreadStart SomeEvent;

   private void NoCompilerWarnings() {
      // This code is here just to make the compiler warnings go away
      SomeEvent.ToString();
      goo.ToString(); 
   }
}


//////////////////////////////// End of File //////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -