home *** CD-ROM | disk | FTP | other *** search
- /*+==========================================================================
- File: reflector.cool
-
- Summary: The purpose of this demo is to introduce COM+ reflection.
- Reflection is a feature in COM+ that allow object inspection
- and dynamic invocation. The reflector program uses the COM+
- reflection to inspect the methods and properties of the class
- named on the command line at runtime. You can also invoke the
- classes methods at runtime by using the command line switches.
-
- Classes: Reflector
-
- Functions: Run, DumpClass, DumpMethods, Usage, main
-
- ----------------------------------------------------------------------------
- This file is part of the Microsoft COM+ 2.0 SDK Samples
-
- Copyright (C) 1998-1999 Microsoft Corporation. All rights reserved.
- ==========================================================================+*/
- using System;
- using System.Reflection;
- using System.Text;
-
-
- class Reflector
- {
- public static String strLoc="000oo";
-
- public bool methods;
- public bool verbose;
- public bool implOnly;
- public bool invoke;
- public String className;
- public String methName;
- public String callArgs;
-
- /*****************************************************************************
- Constructor : Reflector
-
- Abstract: Constructs an instance of Reflector.
-
- Input Parameters: None
-
- Returns: None
- ******************************************************************************/
- public Reflector()
- {
- }
-
- /*****************************************************************************
- Function : Run
-
- Abstract: Dumps the class name and its methods stored in "className".
-
- Input Parameters: None
-
- Returns: int (1==error, 0==OK)
- ******************************************************************************/
- public int Run()
- {
- if (className == null)
- return 1;
-
- try {
- DumpClass();
- }
- catch (Exception e)
- {
- Console.WriteLine("Exception: ({0})\n {1}\n",strLoc, e.StackTrace);
- return 1;
- }
- return 0;
- }
-
- /*****************************************************************************
- Function : DumpClass
-
- Abstract: Dumps the class "className" and its methods.
-
- Input Parameters: None
-
- Returns: Void
- ******************************************************************************/
- public void DumpClass()
- {
- Type t = Type.GetType(className+ "," + className + ".Dll");
- if (t == null) {
- Console.WriteLine("ERROR: Class \"{0}\" not found\n", className);
- return;
- }
- Console.WriteLine("Class: {0}\n",t.FullName);
- if (methods)
- DumpMethods(t);
- }
-
- /*****************************************************************************
- Function : DumpMethods
-
- Abstract: Dumps the class "className" and its methods. If the bool
- invoke is true, then the method name "methName" is invoked
- with the arguments "callArgs".
-
- Input Parameters: c (Microsoft::Runtime::Class in which to dump methods)
-
- Returns: Void
- ******************************************************************************/
- public void DumpMethods(Type t)
- {
- // int in4a = -2;
- strLoc="DM_23n";
- MemberInfo[] memi2 = null;
-
- if (methName == null)
- {
- // t.GetMethods() returns MethodInfo[]
- memi2 = t.GetMethods();
- }
- else
- {
- memi2 = t.FindMembers(MemberTypes.Method , BindingFlags.NonPublic,
- Type.FilterName, methName); // returns MemberInfo[]
- }
-
- Console.WriteLine("Methods ({0})\n",memi2.Length);
-
- strLoc="DM_66n";
-
- if (verbose)
- for (int i=0;i<memi2.Length;i++)
- Console.WriteLine("\t{0}\n",(MethodInfo)memi2[i]);
-
- strLoc="DM_57x";
-
- if (invoke && memi2.Length == 1) {
-
- Object o = Activator.CreateInstance(t);
- Console.WriteLine("Invoking method \"{0}\" on class \"{1}\"\n",
- memi2[0].Name, t.FullName);
-
- Object varRet = null;
- Object[] var2 = null;
-
- if (callArgs != null) {
- String[] str = callArgs.Split( new char[] {' '} );
- var2 = new Object[str.Length];
-
- String a = "";
- if (str.Length != 1)
- a = "s";
- Console.WriteLine("With {0} argument{1}:\n",str.Length,a);
-
- for (int ii = 0; ii < str.Length; ii++) {
- Console.WriteLine("\tArgument {0}: \"{1}\"\n",(ii+1),str[ii]);
-
- var2[ii] = Convert.ToInt32(str[ii]);
- }
- } else {
- Console.WriteLine("With no arguments\n");
- }
-
- strLoc="DM_08u";
-
- try {
- varRet = ((MethodInfo) memi2[0]).Invoke(o,var2);
- }
- catch (Exception e) {
- Console.WriteLine("Invoke Exception: ({0}) {1}\n", strLoc, e);
- }
-
- strLoc="DM_48k";
- Type vt = varRet.GetType();
- Console.WriteLine("Invocation Results: ({0}) ", vt.FullName);
-
- strLoc="DM_92c";
- Console.WriteLine("{0}\n",varRet.ToString());
-
- strLoc="DM_61a";
- }
- }
-
-
- /*****************************************************************************
- Function : Usage
-
- Abstract: Prints usage for reflector application.
-
- Input Parameters: None
-
- Returns: Void
- ******************************************************************************/
- public void Usage()
- {
- Console.WriteLine("Usage: Reflector [options] class\n\n");
- Console.WriteLine("Options:\n");
- Console.WriteLine(" -V\tVerbose\n");
- Console.WriteLine(" -M<opt>\tMethods <=name>\n");
- Console.WriteLine(" -I<opt>\tInvoke <=\"args\">\n");
- Console.WriteLine(" -?\tHelp\n");
-
- }
-
-
- /*****************************************************************************
- Function : main
-
- Abstract: Entry point to the application. Parses command line options
- and instantiates the Reflector class and calls Run().
-
- Input Parameters: argc (number of arguments, including the program name)
- argv (array of ansi strings containing the arguments)
-
- Returns: Void
- ******************************************************************************/
- public static int Main(string[] args)
- {
- Reflector r = new Reflector();
- bool do_usage = false;
-
- try
- {
- if (args.Length < 1) {
- r.Usage();
- return 1;
- }
-
- for (int i=0;i<args.Length;i++) {
- int len = args[i].Length;
- if ( args[i][0] == '/' || args[i][0] == '-' ) {
- switch (Char.ToUpper(args[i][1])) {
- case '?':
- do_usage = true;
- break;
- case 'M':
- r.methods = true;
- if (len > 2) {
- r.methName = args[i].Substring(2);
- }
- break;
- case 'I':
- r.invoke = true;
- if (len > 2) {
- r.callArgs = args[i].Substring(2);
- }
- break;
- case 'V':
- r.verbose = true;
- break;
- default:
- Console.WriteLine("Invalid Option\n\n");
- do_usage = true;
- }
- }
- else {
-
- r.className = args[i];
- }
-
- if (do_usage)
- break;
- }
- if (do_usage || r.className == null) {
- r.Usage();
- return 1;
- }
- return r.Run();
- }
- catch (Exception exc_main)
- {
- Console.WriteLine("Error Err_012ab, exc caught in main, " +
- "strLoc=={0}, exc_main=={1}\n",Reflector.strLoc,exc_main);
- }
- return 1;
- }
- }
-