findinterfaces.cs

来自「Microsoft.NET.框架程序设计修订版中的书中源码」· CS 代码 · 共 92 行

CS
92
字号
/******************************************************************************
Module:  FindInterfaces.cs
Notices: Copyright (c) 2002 Jeffrey Richter
******************************************************************************/


using System;
using System.Reflection;


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


// Define two interfaces for testing
public interface IBookRetailer : IDisposable { 
   void Purchase();
   void ApplyDiscount();
}


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


public interface IMusicRetailer {
   void Purchase();
}


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


// This class implements 2 interfaces defined by this 
// assembly and 1 interface defined by another assembly
class MyRetailer : IBookRetailer, IMusicRetailer {
   public void Purchase() { }
   public void Dispose()  { }
   void IBookRetailer.Purchase()  { }
   public void ApplyDiscount() {}
   void IMusicRetailer.Purchase() { }
}


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


class App {
   static void Main() {
      // Find the interfaces implemented by MyRetailer where
      // the interface is defined in our own assembly. This
      // is accomplished using a delegate to a filter method
      // that we create and pass to FindInterfaces.
      Type t = typeof(MyRetailer);
      Type[] interfaces = t.FindInterfaces(
         new TypeFilter(App.TypeFilter), 
         Assembly.GetCallingAssembly().GetName());
      Console.WriteLine("MyRetailer implements the following " +
         "interfaces (defined in this assembly):");

      // Show information about each interface
      foreach (Type i in interfaces) {
         Console.WriteLine("\nInterface: " + i);

         // Get the type methods that map to the interface's methods
         InterfaceMapping map = t.GetInterfaceMap(i);

         for (Int32 m = 0; m < map.InterfaceMethods.Length; m++) {
            // Display the interface method name and which type 
            // method implements the interface method.
            Console.WriteLine("   {0} is implemented by {1}", 
               map.InterfaceMethods[m], map.TargetMethods[m]);
         }
      }

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


   // This filter delegate method takes a type and an object, performs
   // some check and returns true if the type is to be included 
   // in the array of returned types
   static Boolean TypeFilter(Type t, Object filterCriteria) {
      // Return true if the interface is defined in the same 
      // assembly identified by filterCriteria
      return t.Assembly.GetName().ToString() == 
         filterCriteria.ToString();
   }
}


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

⌨️ 快捷键说明

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