home *** CD-ROM | disk | FTP | other *** search
- /*+==========================================================================
- File: reflector.cpp
-
- Summary: The purpose of this demo is to introduce NGWS reflection.
- Reflection is a feature in NGWS that allow object inspection
- and dynamic invocation. The reflector program uses the NGWS
- 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 NGWS Samples.
-
- Copyright (C) 1998-2000 Microsoft Corporation. All rights reserved.
- ==========================================================================+*/
- #using <mscorlib.dll>
-
- using namespace System;
- using namespace System::Reflection;
- using namespace System::Text;
-
- #define NULL 0
-
- [sysimport(dll="msvcrt", charset="ansi")]
- extern "C" char* _cdecl strcpy(StringBuilder *dest, char *src);
-
- [sysimport(dll="msvcrt", charset="ansi")]
- extern "C" int _cdecl strlen(char *str);
-
- [sysimport(dll="msvcrt", charset="ansi")]
- extern "C" char _cdecl toupper(char c);
-
- [sysimport(dll="msvcrt", charset="ansi")]
- extern "C" int _cdecl printf(String *fmt, ...);
-
- __gc class Reflector
- {
- public:
- static String *strLoc=L"000oo";
-
- bool methods;
- bool verbose;
- bool implOnly;
- bool invoke;
- String *className;
- String *methName;
- String *callArgs;
-
- /*****************************************************************************
- Constructor : Reflector
-
- Abstract: Constructs an instance of Reflector.
-
- Input Parameters: None
-
- Returns: None
- ******************************************************************************/
- Reflector() : methods(false), verbose(false),
- implOnly(false), invoke(false),
- className(NULL), methName(NULL),
- callArgs(NULL)
- {
- }
-
- /*****************************************************************************
- Function : Run
-
- Abstract: Dumps the class name and its methods stored in "className".
-
- Input Parameters: None
-
- Returns: int (1==error, 0==OK)
- ******************************************************************************/
- int Run()
- {
- if (className == NULL)
- return 1;
-
- try {
- DumpClass();
- }
- catch (Exception *e)
- {
- printf(L"Exception: (%s)\n",strLoc);
- printf(L"%s\n",e->StackTrace);
- return 1;
- }
- return 0;
- }
-
- /*****************************************************************************
- Function : DumpClass
-
- Abstract: Dumps the class "className" and its methods.
-
- Input Parameters: None
-
- Returns: Void
- ******************************************************************************/
- void DumpClass()
- {
- String *clsName = String::Concat(className, ",");
- clsName = String::Concat(clsName, className , ".dll");
-
- Type *t = Type::GetType(clsName);
- if (t == NULL) {
- printf(L"ERROR: Class \"%s\" not found\n", className);
- return;
- }
- printf(L"Class: %s\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
- ******************************************************************************/
- void DumpMethods(Type *t)
- {
- int in4a = -2;
- strLoc=L"DM_23n";
- MemberInfo *memi2 __gc[] = NULL;
-
- if (methName == NULL)
- {
- // t->GetMethods() returns MethodInfo[]
- memi2 = t->GetMethods();
- }
- else
- {
- memi2 = t->FindMembers(MemberTypes::Method , BindingFlags::NonPublic,
- Type::FilterName, methName); // returns MemberInfo[]
- }
-
- printf(L"Methods (%s)\n",Convert::ToString(memi2->Length));
-
- strLoc=L"DM_66n";
-
- if (verbose)
- for (int i=0;i<memi2->Length;i++)
- printf(L"\t%s\n",(memi2[i])->ToString());
-
- strLoc=L"DM_57x";
-
- if (invoke && memi2->Length == 1) {
-
- Object *o = Activator::CreateInstance(t);
- printf(L"Invoking method \"%s\" on class \"%s\"\n",
- memi2[0]->Name, t->FullName);
-
- Object *varRet = NULL;
- Object *var2 __gc[] = (Object*)NULL;
-
- if (callArgs != NULL) {
- // Construct parameter list.
- wchar_t split __gc[] = new wchar_t __gc[1];
- split[0] = L' ';
- String *str __gc[] = callArgs->Split(split);
- var2 = new Object* __gc[str->Length];
-
- String *a = L"";
- if (str->Length != 1)
- a = L"s";
- printf(L"With %ld argument%s:\n",str->Length,a);
-
- for (int ii = 0; ii < str->Length; ii++) {
- printf(L"\tArgument %ld: \"%s\"\n",(ii+1),str[ii]);
-
- var2[ii] = (dynamic_cast<Object *> (Convert::ToInt32(str[ii])));
- }
- } else {
- printf(L"With no arguments\n");
- }
-
- strLoc=L"DM_08u";
-
- try {
- varRet = (static_cast<MethodInfo*> (memi2[0]))->Invoke(o,var2);
- }
- catch (Exception *e) {
- printf(L"Invoke Exception: (%s) %s\n", strLoc, e->ToString());
- }
-
- strLoc=L"DM_48k";
- Type *vt = varRet->GetType();
- printf(L"Invocation Results: (%s) ", vt->FullName);
-
- strLoc=L"DM_92c";
- printf(L"%s\n",varRet->ToString());
-
- strLoc=L"DM_61a";
- }
- }
-
-
- };
-
- /*****************************************************************************
- Function : Usage
-
- Abstract: Prints usage for reflector application.
-
- Input Parameters: None
-
- Returns: Void
- ******************************************************************************/
- void Usage()
- {
- printf(L"Usage: Reflector [options] class\n\n");
- printf(L"Options:\n");
- printf(L" -V\tVerbose\n");
- printf(L" -M<opt>\tMethods <=name>\n");
- printf(L" -I<opt>\tInvoke <=\"args\">\n");
- printf(L" -?\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
- ******************************************************************************/
- int main(int argc, char **args)
- {
- Reflector *r = new Reflector();
- bool do_usage = false;
-
- try
- {
- if (argc < 2) {
- Usage();
- return 1;
- }
-
- for (int i=1;i<argc;i++) {
- int len = strlen(args[i]);
- if ((args[i][0] == L'/') || (args[i][0] == L'-')) {
- switch (toupper(args[i][1])) {
- case L'?':
- do_usage = true;
- break;
- case L'M':
- r->methods = true;
- if (len > 2) {
- len -= 2;
- StringBuilder *sb = new StringBuilder(256);
- strcpy(sb,args[i]+2);
- r->methName = sb->ToString();
- }
- break;
- case L'I':
- r->invoke = true;
- if (len > 2) {
- len -= 2;
- StringBuilder *sb = new StringBuilder(256);
- strcpy(sb,args[i]+2);
- r->callArgs = sb->ToString();
- }
- break;
- case L'V':
- r->verbose = true;
- break;
- default:
- printf(L"Invalid Option\n\n");
- do_usage = true;
- }
- }
- else {
- StringBuilder *sb = new StringBuilder(256);
- strcpy(sb,args[i]);
- r->className = sb->ToString();
- }
- if (do_usage)
- break;
- }
- if (do_usage || r->className == NULL) {
- Usage();
- return 1;
- }
- return r->Run();
- }
- catch (Exception *exc_main)
- {
- printf(L"Error Err_012ab, exc caught in main, strLoc==%s, "
- L"exc_main==%s\n",r->strLoc,exc_main->ToString());
- }
- return 1;
- }
-
-