home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / Reflector / CS / reflector.cs < prev   
Encoding:
Text File  |  2000-06-23  |  7.0 KB  |  275 lines

  1. /*+==========================================================================
  2.   File:      reflector.cool
  3.  
  4.   Summary:   The purpose of this demo is to introduce COM+ reflection. 
  5.              Reflection is a feature in COM+ that allow object inspection 
  6.              and dynamic invocation. The reflector program uses the COM+ 
  7.              reflection to inspect the methods and properties of the class 
  8.              named on the command line at runtime. You can also invoke the 
  9.              classes methods at runtime by using the command line switches.
  10.  
  11.   Classes:   Reflector
  12.  
  13.   Functions: Run, DumpClass, DumpMethods, Usage, main
  14.  
  15. ----------------------------------------------------------------------------
  16.   This file is part of the Microsoft COM+ 2.0 SDK Samples
  17.  
  18.   Copyright (C) 1998-1999 Microsoft Corporation.  All rights reserved.
  19. ==========================================================================+*/
  20. using System;
  21. using System.Reflection;
  22. using System.Text;
  23.  
  24.  
  25. class Reflector
  26. {
  27.     public static String strLoc="000oo";
  28.  
  29.     public bool methods;
  30.     public bool verbose;
  31.     public bool implOnly;
  32.     public bool invoke;
  33.     public String className;
  34.     public String methName;
  35.     public String callArgs;
  36.  
  37. /*****************************************************************************
  38.  Constructor : Reflector
  39.  
  40.  Abstract:     Constructs an instance of Reflector.
  41.             
  42.  Input Parameters: None
  43.  
  44.  Returns: None
  45. ******************************************************************************/
  46.     public Reflector()
  47.     {
  48.     }
  49.  
  50. /*****************************************************************************
  51.  Function :    Run
  52.  
  53.  Abstract:     Dumps the class name and its methods stored in "className".
  54.             
  55.  Input Parameters: None
  56.  
  57.  Returns: int (1==error, 0==OK)
  58. ******************************************************************************/
  59.     public int Run()
  60.     {
  61.         if (className == null)
  62.             return 1;
  63.  
  64.         try {
  65.             DumpClass();
  66.         }
  67.         catch (Exception e)
  68.         {
  69.             Console.WriteLine("Exception: ({0})\n {1}\n",strLoc, e.StackTrace);
  70.             return 1;
  71.         }
  72.         return 0;
  73.     }
  74.  
  75. /*****************************************************************************
  76.  Function :    DumpClass
  77.  
  78.  Abstract:     Dumps the class "className" and its methods.
  79.             
  80.  Input Parameters: None
  81.  
  82.  Returns: Void
  83. ******************************************************************************/
  84.     public void DumpClass()
  85.     {
  86.         Type t = Type.GetType(className+ "," + className + ".Dll");
  87.         if (t == null) {
  88.             Console.WriteLine("ERROR: Class \"{0}\" not found\n", className);
  89.             return;
  90.         }
  91.         Console.WriteLine("Class: {0}\n",t.FullName);
  92.         if (methods)
  93.             DumpMethods(t);
  94.     }
  95.  
  96. /*****************************************************************************
  97.  Function :    DumpMethods
  98.  
  99.  Abstract:     Dumps the class "className" and its methods.  If the bool 
  100.                invoke is true, then the method name "methName" is invoked
  101.                with the arguments "callArgs".
  102.             
  103.  Input Parameters: c (Microsoft::Runtime::Class in which to dump methods)
  104.  
  105.  Returns: Void
  106. ******************************************************************************/
  107.     public void DumpMethods(Type t)
  108.     {
  109.         // int in4a = -2;
  110.         strLoc="DM_23n";
  111.         MemberInfo[] memi2 = null;
  112.         
  113.         if (methName == null)
  114.         {
  115.             // t.GetMethods() returns MethodInfo[]
  116.             memi2 = t.GetMethods();
  117.         }
  118.         else
  119.         {
  120.             memi2 = t.FindMembers(MemberTypes.Method ,  BindingFlags.NonPublic, 
  121.                     Type.FilterName, methName); // returns MemberInfo[]
  122.         }
  123.     
  124.         Console.WriteLine("Methods ({0})\n",memi2.Length);
  125.  
  126.         strLoc="DM_66n";
  127.  
  128.         if (verbose)
  129.             for (int i=0;i<memi2.Length;i++)
  130.                 Console.WriteLine("\t{0}\n",(MethodInfo)memi2[i]);
  131.  
  132.         strLoc="DM_57x";
  133.  
  134.         if (invoke && memi2.Length == 1) {
  135.  
  136.             Object o = Activator.CreateInstance(t);
  137.             Console.WriteLine("Invoking method \"{0}\" on class \"{1}\"\n", 
  138.                     memi2[0].Name, t.FullName);
  139.  
  140.             Object  varRet = null;
  141.             Object[] var2   = null;
  142.             
  143.             if (callArgs != null) {
  144.                 String[] str = callArgs.Split( new char[] {' '} );
  145.                 var2 = new Object[str.Length];
  146.  
  147.                 String a = "";
  148.                 if (str.Length != 1)
  149.                     a = "s";
  150.                 Console.WriteLine("With {0} argument{1}:\n",str.Length,a);
  151.  
  152.                 for (int ii = 0; ii < str.Length; ii++) {
  153.                     Console.WriteLine("\tArgument {0}: \"{1}\"\n",(ii+1),str[ii]);
  154.  
  155.                     var2[ii] = Convert.ToInt32(str[ii]);
  156.                 }                    
  157.             } else {
  158.                 Console.WriteLine("With no arguments\n");
  159.             }
  160.                 
  161.             strLoc="DM_08u";
  162.  
  163.             try {
  164.                 varRet = ((MethodInfo) memi2[0]).Invoke(o,var2);
  165.             }
  166.             catch (Exception e) {
  167.                 Console.WriteLine("Invoke Exception: ({0}) {1}\n", strLoc, e);
  168.             }
  169.  
  170.             strLoc="DM_48k";
  171.             Type vt = varRet.GetType();
  172.             Console.WriteLine("Invocation Results: ({0}) ", vt.FullName);
  173.  
  174.             strLoc="DM_92c";
  175.             Console.WriteLine("{0}\n",varRet.ToString());
  176.  
  177.             strLoc="DM_61a";
  178.         }
  179.     }
  180.  
  181.  
  182. /*****************************************************************************
  183.  Function :    Usage
  184.  
  185.  Abstract:     Prints usage for reflector application.
  186.             
  187.  Input Parameters: None
  188.  
  189.  Returns: Void
  190. ******************************************************************************/
  191. public void Usage()
  192. {
  193.     Console.WriteLine("Usage: Reflector [options] class\n\n");
  194.     Console.WriteLine("Options:\n");
  195.     Console.WriteLine("   -V\tVerbose\n");
  196.     Console.WriteLine("   -M<opt>\tMethods <=name>\n");
  197.     Console.WriteLine("   -I<opt>\tInvoke <=\"args\">\n");
  198.     Console.WriteLine("   -?\tHelp\n");
  199.  
  200. }
  201.  
  202.  
  203. /*****************************************************************************
  204.  Function :    main
  205.  
  206.  Abstract:     Entry point to the application.  Parses command line options
  207.                and instantiates the Reflector class and calls Run().
  208.             
  209.  Input Parameters: argc (number of arguments, including the program name)
  210.                    argv (array of ansi strings containing the arguments)
  211.  
  212.  Returns: Void
  213. ******************************************************************************/
  214. public static int Main(string[] args)
  215. {
  216.     Reflector r = new Reflector();
  217.     bool do_usage = false;
  218.     
  219.     try
  220.     {
  221.         if (args.Length < 1) {
  222.             r.Usage();
  223.             return 1;
  224.         }
  225.  
  226.         for (int i=0;i<args.Length;i++) {
  227.             int len = args[i].Length;
  228.             if ( args[i][0] == '/' || args[i][0] == '-' ) {
  229.                 switch (Char.ToUpper(args[i][1])) {
  230.                 case '?':
  231.                     do_usage = true;
  232.                     break;
  233.                 case 'M':
  234.                     r.methods = true;
  235.                     if (len > 2) {
  236.                         r.methName = args[i].Substring(2);
  237.                     }
  238.                     break;
  239.                 case 'I':
  240.                     r.invoke = true;
  241.                     if (len > 2) {
  242.                         r.callArgs = args[i].Substring(2);
  243.                     }
  244.                     break;
  245.                 case 'V':
  246.                     r.verbose = true;
  247.                     break;
  248.                 default:
  249.                     Console.WriteLine("Invalid Option\n\n");
  250.                     do_usage = true;
  251.                 }
  252.             }
  253.             else {
  254.                 
  255.                 r.className = args[i];
  256.             }
  257.             
  258.             if (do_usage)
  259.                 break;
  260.         }
  261.         if (do_usage || r.className == null) {
  262.             r.Usage();
  263.             return 1;
  264.         }
  265.         return r.Run();
  266.     }
  267.     catch (Exception exc_main)
  268.     {
  269.         Console.WriteLine("Error Err_012ab, exc caught in main, " +
  270.                           "strLoc=={0}, exc_main=={1}\n",Reflector.strLoc,exc_main);
  271.     }
  272.     return 1;
  273. }
  274. }
  275.