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. Something to reflect upon...

Something to reflect upon...

Scheduled Pinned Locked Moved Managed C++/CLI
c++performancehelptutorialquestion
5 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.
  • S Offline
    S Offline
    Sandeep Datta
    wrote on last edited by
    #1

    Hi, Please consider the following code fragment in C++/CLI (I am quoting from memory )... Assembly^ a = Assembly::LoadFile("MyTypes.dll"); Type^ myType = a->GetType("MyNamespace.MyClass"); Object^ obj = Activator::CreateInstance(myType); IMyInterface^ itf = (IMyInterface^)obj; The last line throws an invalid cast exception even if MyClass implements IMyInterface. Any idea whats going on and how to solve this problem?

    SDX2000

    G 1 Reply Last reply
    0
    • S Sandeep Datta

      Hi, Please consider the following code fragment in C++/CLI (I am quoting from memory )... Assembly^ a = Assembly::LoadFile("MyTypes.dll"); Type^ myType = a->GetType("MyNamespace.MyClass"); Object^ obj = Activator::CreateInstance(myType); IMyInterface^ itf = (IMyInterface^)obj; The last line throws an invalid cast exception even if MyClass implements IMyInterface. Any idea whats going on and how to solve this problem?

      SDX2000

      G Offline
      G Offline
      George L Jackson
      wrote on last edited by
      #2

      Where is "IMyInterface" located? Is it in "MyTypes.dll"? If not, is the assembly containing "IMyInterface" common to the current code and the loaded assembly? Are you using a "using namespace MyTypes;" somewhere in your code?

      "We make a living by what we get, we make a life by what we give." --Winston Churchill

      S 1 Reply Last reply
      0
      • G George L Jackson

        Where is "IMyInterface" located? Is it in "MyTypes.dll"? If not, is the assembly containing "IMyInterface" common to the current code and the loaded assembly? Are you using a "using namespace MyTypes;" somewhere in your code?

        "We make a living by what we get, we make a life by what we give." --Winston Churchill

        S Offline
        S Offline
        Sandeep Datta
        wrote on last edited by
        #3

        Hi George, Thanks for responding. IMyInterface is located in the same assembly (say MyLoader) which is trying to load MyTypes.dll, in other words its not located in MyTypes.dll. A reference to MyLoader has been added while compiling MyTypes.dll. I am not using "using namespace MyTypes;" anywhere. Actually I don't want to. Doing so will defeat the purpose of interface based programming. You could think of this as a plugin based application where the main application has been written in C++/CLI and the plugins can be written in any language. Its not possible for me to add the references to all present and future plugins while compiling MyLoader. Regards, SDX.

        SDX2000

        G 1 Reply Last reply
        0
        • S Sandeep Datta

          Hi George, Thanks for responding. IMyInterface is located in the same assembly (say MyLoader) which is trying to load MyTypes.dll, in other words its not located in MyTypes.dll. A reference to MyLoader has been added while compiling MyTypes.dll. I am not using "using namespace MyTypes;" anywhere. Actually I don't want to. Doing so will defeat the purpose of interface based programming. You could think of this as a plugin based application where the main application has been written in C++/CLI and the plugins can be written in any language. Its not possible for me to add the references to all present and future plugins while compiling MyLoader. Regards, SDX.

          SDX2000

          G Offline
          G Offline
          George L Jackson
          wrote on last edited by
          #4

          Please note that two identical interfaces created in two different assemblies are not the same type. Thus, you will get casting errors. Using namespaces does not defeat the purpose of interface programming. The .NET Framework is an example of that.

          "We make a living by what we get, we make a life by what we give." --Winston Churchill

          S 1 Reply Last reply
          0
          • G George L Jackson

            Please note that two identical interfaces created in two different assemblies are not the same type. Thus, you will get casting errors. Using namespaces does not defeat the purpose of interface programming. The .NET Framework is an example of that.

            "We make a living by what we get, we make a life by what we give." --Winston Churchill

            S Offline
            S Offline
            Sandeep Datta
            wrote on last edited by
            #5

            George, I think I was bit hasty in describing my problem. I agree with you "Using namespaces does not defeat the purpose of interface programming." but that is not what I meant. Thanks for your help anyway. I have found the reason why I was getting an Invalid cast exception. An invalid cast exception may be generated due to missing assemblies! (consider Assembly1::Class1 extends Assembly2::Interface1). (Note:Assemblies can be in the same folder and still be missing!) refer suzzane cooks blog http://blogs.msdn.com/suzcook/archive/2004/06/02/debugging-an-invalidcastexception.aspx

            The best way to accelerate a Macintosh is at 9.8m/sec-sec. - Marcus Dolengo

            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