The following code demonstrates the enumeration of public types and members using
Given the name of an assembly, either in the local directory or in the GAC, the code below attempts to open the assembly and retrieve descriptions. If successful, each type is displayed with its public members.
Note that
Example
В | Copy Code |
---|---|
// self_reflection.cpp // compile with: /clr using namespace System; using namespace System::Reflection; using namespace System::Collections; public ref class ExampleType { public: ExampleType() {} void Func() {} }; int main() { String^ delimStr = " "; array<Char>^ delimiter = delimStr->ToCharArray( ); array<String^>^ args = Environment::CommandLine->Split( delimiter ); Assembly^ a = Assembly::LoadFrom("self_reflection.exe"); Console::WriteLine(a); int count = 0; array<Type^>^ types = a->GetTypes(); IEnumerator^ typeIter = types->GetEnumerator(); while ( typeIter->MoveNext() ) { Type^ t = dynamic_cast<Type^>(typeIter->Current); Console::WriteLine(" {0}", t->ToString()); array<MemberInfo^>^ members = t->GetMembers(); IEnumerator^ memberIter = members->GetEnumerator(); while ( memberIter->MoveNext() ) { MemberInfo^ mi = dynamic_cast<MemberInfo^>(memberIter->Current); Console::Write(" {0}", mi->ToString( ) ); if (mi->MemberType == MemberTypes::Constructor) Console::Write(" (constructor)"); Console::WriteLine(); } count++; } Console::WriteLine("{0} types found", count); } |