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. C#
  4. downcasting Object types

downcasting Object types

Scheduled Pinned Locked Moved C#
oopquestion
8 Posts 6 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Someone please tell me if this is supposed to work: (because it compiles and all I'm getting is a runtime invalid cast exception)Object obj = someAssembly.CreateInstance("MyClass"); MyAbstractClass x = (MyAbstractClass) obj; //downcasting allowed?? x.doCoolStuff(); //exploit polymorphism
    More importantly, I don't want to use Invoke() calls. Isn't there a cleaner work-around? Bilal

    S P F 3 Replies Last reply
    0
    • L Lost User

      Someone please tell me if this is supposed to work: (because it compiles and all I'm getting is a runtime invalid cast exception)Object obj = someAssembly.CreateInstance("MyClass"); MyAbstractClass x = (MyAbstractClass) obj; //downcasting allowed?? x.doCoolStuff(); //exploit polymorphism
      More importantly, I don't want to use Invoke() calls. Isn't there a cleaner work-around? Bilal

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

      What's the definition of MyClass and MyAbstractClass? Also, have you tried this without using CreateInstance to see if that works? Cheers, Simon "VB.NET ... the STD of choice", Me, interal company memo

      L 1 Reply Last reply
      0
      • S SimonS

        What's the definition of MyClass and MyAbstractClass? Also, have you tried this without using CreateInstance to see if that works? Cheers, Simon "VB.NET ... the STD of choice", Me, interal company memo

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        MyClass is derived from MyAbstractClass (sorry, should have mentioned that) and implements parent's abstract functions. What alternatives are there for creating objects at runtime from definitions available only in other .NET DLLs or EXEs? I'm just a .NET newbie :-O Bilal

        E J 2 Replies Last reply
        0
        • L Lost User

          Someone please tell me if this is supposed to work: (because it compiles and all I'm getting is a runtime invalid cast exception)Object obj = someAssembly.CreateInstance("MyClass"); MyAbstractClass x = (MyAbstractClass) obj; //downcasting allowed?? x.doCoolStuff(); //exploit polymorphism
          More importantly, I don't want to use Invoke() calls. Isn't there a cleaner work-around? Bilal

          P Offline
          P Offline
          Paul Riley
          wrote on last edited by
          #4

          Can't imagine why this wouldn't work. Try this:

          MyAbstractClass x = obj as MyAbstractClass;
          

          as is a lot more flexible than direct casting and if it can't cast then rather than throwing an exception it will set x to null, giving you some room to debug. Paul I think there're pieces of me you've never seen - Tori Amos, Tear in Your Hand

          1 Reply Last reply
          0
          • L Lost User

            MyClass is derived from MyAbstractClass (sorry, should have mentioned that) and implements parent's abstract functions. What alternatives are there for creating objects at runtime from definitions available only in other .NET DLLs or EXEs? I'm just a .NET newbie :-O Bilal

            E Offline
            E Offline
            Eric Gunnerson msft
            wrote on last edited by
            #5

            You might want to look at the Activator class. Note that if you want to cast to an abstract class you own, the class will have to refer to the same abstract class (ie same assembly and same version)

            1 Reply Last reply
            0
            • L Lost User

              MyClass is derived from MyAbstractClass (sorry, should have mentioned that) and implements parent's abstract functions. What alternatives are there for creating objects at runtime from definitions available only in other .NET DLLs or EXEs? I'm just a .NET newbie :-O Bilal

              J Offline
              J Offline
              John Fisher
              wrote on last edited by
              #6

              As Eric allueded to, you've got to watch out when using objects from other assemblies. Unless you're using an interface or class type shared from the same .dll, you'll run into problems. In other words, the assembly creating the object and the assembly trying to downcast the object must both reference a separate assembly that describes the interface or class to which you are downcasting. (Does that make sense?) :D John

              L 1 Reply Last reply
              0
              • J John Fisher

                As Eric allueded to, you've got to watch out when using objects from other assemblies. Unless you're using an interface or class type shared from the same .dll, you'll run into problems. In other words, the assembly creating the object and the assembly trying to downcast the object must both reference a separate assembly that describes the interface or class to which you are downcasting. (Does that make sense?) :D John

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                It does and you two clinched it. Thanx guys! Bilal

                1 Reply Last reply
                0
                • L Lost User

                  Someone please tell me if this is supposed to work: (because it compiles and all I'm getting is a runtime invalid cast exception)Object obj = someAssembly.CreateInstance("MyClass"); MyAbstractClass x = (MyAbstractClass) obj; //downcasting allowed?? x.doCoolStuff(); //exploit polymorphism
                  More importantly, I don't want to use Invoke() calls. Isn't there a cleaner work-around? Bilal

                  F Offline
                  F Offline
                  Feng Qin
                  wrote on last edited by
                  #8

                  The following is more safer: Object obj = someAssembly.CreateInstance("MyClass"); if( obj is MyAbstractClass ) { MyAbstractClass x = (MyAbstractClass) obj; //downcasting allowed?? x.doCoolStuff(); //exploit polymorphism } and the precondition is you have added reference library, which has a definition of MyAbstractClass.:cool: I'm amumu, and you?

                  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