Reflection how to cast to loaded type
-
hi all
System::Reflection::Assembly^ SampleAssembly; System::Type^ myType; System::Activator::Object^ myObj; try { SampleAssembly = System::Reflection::Assembly::LoadFrom( Path ); } catch (System::IO::FileNotFoundException^ catchException) { Form->_TabCon->_PageStatus->StatusText->Text += catchException->ToString() + System::Environment::NewLine; return 1; } try { myType = SampleAssembly->GetType("Memory.What"); if (!myType) throw (System::String^) "Can´t locate Base Function [incompatible plug ???]"; } catch (System::String^ catchException) { Form->_TabCon->_PageStatus->StatusText->Text += catchException + System::Environment::NewLine; return 1; } myObj = System::Activator::CreateInstance( myType ); // creates the instance **What^ W = (What^)myObj;**
how can i cast to "What^" ? i tryed to have a .h .cpp file whit the same declaration as in "Memmory.what" but i get an error An unhandled exception of type 'System.InvalidCastException' occurred in WhatsUp.exe Additional information: Unable to cast object of type 'Memory.What' to type 'WhatsUp.What'. -
hi all
System::Reflection::Assembly^ SampleAssembly; System::Type^ myType; System::Activator::Object^ myObj; try { SampleAssembly = System::Reflection::Assembly::LoadFrom( Path ); } catch (System::IO::FileNotFoundException^ catchException) { Form->_TabCon->_PageStatus->StatusText->Text += catchException->ToString() + System::Environment::NewLine; return 1; } try { myType = SampleAssembly->GetType("Memory.What"); if (!myType) throw (System::String^) "Can´t locate Base Function [incompatible plug ???]"; } catch (System::String^ catchException) { Form->_TabCon->_PageStatus->StatusText->Text += catchException + System::Environment::NewLine; return 1; } myObj = System::Activator::CreateInstance( myType ); // creates the instance **What^ W = (What^)myObj;**
how can i cast to "What^" ? i tryed to have a .h .cpp file whit the same declaration as in "Memmory.what" but i get an error An unhandled exception of type 'System.InvalidCastException' occurred in WhatsUp.exe Additional information: Unable to cast object of type 'Memory.What' to type 'WhatsUp.What'.Try this:
What^ W = safe_cast<What^> (myObj);
-
Try this:
What^ W = safe_cast<What^> (myObj);
unfortunaly no :( same error this is where i am in the moment : myType = SampleAssembly->GetType("Memory.What"); myObj = System::Activator::CreateInstance( myType ); System::Reflection::MethodInfo^ mi = myType->GetMethod("Ver"); mi->Invoke(myObj , nullptr ); so i can call the methode but its not to elegant there muss by some other way so that i can call it like : LoadedDLL->Ver();