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

📄 dynamicdelegates.cs

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


using System;
using System.Reflection;
using System.IO;


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


// Here are some different delegate definitions
delegate Object TwoInt32s(Int32  n1, Int32  n2);
delegate Object OneString(String s1);


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


class App {
   static void Main(String[] args) {
      if (args.Length < 2) {
         String fileName  = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().CodeBase);
         Console.WriteLine("Usage:");
         Console.WriteLine("{0} delType methodName [Param1] [Param2]", fileName);
         Console.WriteLine("   where delType must be TwoInt32s or OneString");
         Console.WriteLine("   if delType is TwoInt32s, methodName must be Add or Subtract");
         Console.WriteLine("   if delType is OneString, methodName must be NumChars or Reverse");
         Console.WriteLine();
         Console.WriteLine("Examples:");
         Console.WriteLine("   {0} TwoInt32s Add 123 321", fileName);
         Console.WriteLine("   {0} TwoInt32s Subtract 123 321", fileName);
         Console.WriteLine("   {0} OneString NumChars \"Hello there\"", fileName);
         Console.WriteLine("   {0} OneString Reverse  \"Hello there\"", fileName);
         return;
      }
      Type delType = Type.GetType(args[0]);
      if (delType == null) {
         Console.WriteLine("Invalid delType argument: " + args[0]);
         return;
      }

      Delegate d;
      try {
         d = Delegate.CreateDelegate(delType, typeof(App), args[1]);
      }
      catch (ArgumentException) {
         Console.WriteLine("Invalid methodName argument: " + args[1]);
         return;
      }

      Object[] callbackArgs = new Object[args.Length - 2];
      if (d.GetType() == typeof(TwoInt32s)) {
         try {
            for (Int32 a = 2; a < args.Length; a++) 
               callbackArgs[a - 2] = Int32.Parse(args[a]);
         }
         catch (FormatException) {
            Console.WriteLine("Parameters must be integers.");
            return;
         }
      }
      
      if (d.GetType() == typeof(OneString)) {
         Array.Copy(args, 2, callbackArgs, 0, callbackArgs.Length);
      }

      try {
         Object result = d.DynamicInvoke(callbackArgs);
         Console.WriteLine("Result = " + result);
      }
      catch (TargetParameterCountException) {
         Console.WriteLine("Incorrect number of parameters specified.");
      }
   }


   // This callback method takes 2 Int32 parameters
   static Object Add(Int32 n1, Int32 n2) { 
      return n1 + n2;
   }

   // This callback method takes 2 Int32 parameters
   static Object Subtract(Int32 n1, Int32 n2) { 
      return n1 - n2;
   }

   // This callback method takes 1 String parameter
   static Object NumChars(String s1) { 
      return s1.Length;
   }

   // This callback method takes 1 String parameter
   static Object Reverse(String s1) { 
      Char[] chars = s1.ToCharArray();
      Array.Reverse(chars);
      return new String(chars);
   }
}


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

⌨️ 快捷键说明

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