4 // Dynamic invocation helper classes. The details of how
5 // to access the .NET object model via the Reflection API
6 // is taken care of by Invoker.{h,cpp}
11 using namespace System;
12 using namespace System::Reflection;
13 using namespace System::Text;
14 using namespace System::Runtime::InteropServices;
16 [assembly:AssemblyKeyFileAttribute(S"invoker.snk")];
23 // Purpose: pairing up an assembly name and the type/class name.
26 public __gc class TypeName {
29 System::String* m_assembly;
30 System::String* m_class;
34 m_assembly = String::Empty;
35 m_class = String::Empty;
40 if (m_assembly && m_assembly != String::Empty ) {
42 Console::Write(m_assembly);
45 Console::WriteLine(m_class);
48 int Length() { return m_length; }
50 System::String* toStdString() {
51 System::String* res = new System::String(m_class->ToCharArray());
53 if (m_assembly && m_assembly != String::Empty ){
54 res = String::Concat(res, S",");
55 res = String::Concat(res, m_assembly);
62 // Class: InvokeBridge
64 // Purpose: Collection of (static) methods for dynamically creating
65 // objects and accessing methods/fields on them.
67 [ClassInterface(ClassInterfaceType::AutoDual),
68 GuidAttribute("39D497D9-60E0-3525-B7F2-7BC096D3A2A3"),
71 public __gc class InvokeBridge {
74 Assembly* corAss = Assembly::Load("mscorlib.dll");
75 System::String* dir = System::IO::Path::GetDirectoryName(corAss->Location);
77 m_assemblies = new System::Collections::ArrayList();
79 System::String* fs[] = System::IO::Directory::GetFiles(dir, "*.dll");
80 for (int i=0;i < fs->Length; i++) {
82 Assembly* tAss = Assembly::LoadFrom(fs[i]);
83 m_assemblies->Add(tAss->FullName);
84 } catch (Exception* e) {
91 // Method: CreateObject(String* assemName, String* objSpec, Object* args[])
93 // Purpose: Given a fully qualified name of a class/type, try
94 // to create an instance of it.
96 Object* CreateObject(System::String* assemName,
97 System::String* objSpec,
101 // Method: InvokeMethod
103 // Purpose: Given a pointer to an already created object, look up
104 // one of its method. If found, invoke the method passing it
105 // 'args' as arguments.
107 // Comments: the format of the method-spec is "methodName(type1,..,typeN)" [N>=0]
109 Object* InvokeMethod(Object* obj,
110 System::String* methSpec,
114 // Method: InvokeStaticMethod
116 // Purpose: Invoke a static method, given the fully qualified name
117 // of the method (and its arguments). If found, invoke the
118 // method passing it 'args' as arguments.
120 // Comments: the format of the method-spec is
121 // "T1.T2.<..>.Tn.methodName(type1,..,typeN)" [N>=0]
123 Object* InvokeStaticMethod(System::String* assemName,
124 System::String* methSpec,
130 // Purpose: Fetch the (boxed) value of named field of a given object.
132 Object* GetField(Object* obj, System::String* fieldSpec);
137 // Purpose: Fetch the (boxed) value of named static field.
139 Object* GetStaticField(System::String* clsName,
140 System::String* fieldSpec);
145 // Purpose: Replace the (boxed) value of named field of a given object.
147 void SetField(Object* obj, System::String* fieldSpec, Object* val);
150 // Method: SetStaticField
152 // Purpose: Replace the (boxed) value of named field of a given object.
154 void SetStaticField(System::String* clsName,
155 System::String* fieldSpec,
162 // Purpose: construct a System.String object copy in a manner that avoids
163 // COM Interop from deconstructing it to a BSTR.
165 System::Object* NewString( System::String* s);
168 // Method: NewArgArray
170 // Purpose: create a new array for holding (boxed) arguments to constructors/
173 Array* NewArgArray(int sz);
178 // Purpose: set an entry in the argument vector.
180 void SetArg(Object* arr[], Object* val, int idx);
185 // Purpose: get an entry in the argument vector.
187 Object* GetArg(Object* arr[], int idx);
189 System::Type* InvokeBridge::GetType(System::String* typeName);
192 System::Collections::ArrayList __gc* m_assemblies;
193 Object* InvokeBridge::CreateInstance(TypeName* typeName,