Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Reflection how to cast to loaded type

Reflection how to cast to loaded type

Scheduled Pinned Locked Moved Managed C++/CLI
questionc++performancehelptutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    Xeef
    wrote on last edited by
    #1

    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'.

    S 1 Reply Last reply
    0
    • X Xeef

      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'.

      S Offline
      S Offline
      sarah_malik
      wrote on last edited by
      #2

      Try this: What^ W = safe_cast<What^> (myObj);

      X 1 Reply Last reply
      0
      • S sarah_malik

        Try this: What^ W = safe_cast<What^> (myObj);

        X Offline
        X Offline
        Xeef
        wrote on last edited by
        #3

        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();

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups