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. Runtime cast down inheritance tree

Runtime cast down inheritance tree

Scheduled Pinned Locked Moved C#
questionwpfdata-structuresoophelp
3 Posts 3 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.
  • G Offline
    G Offline
    Gizz
    wrote on last edited by
    #1

    Hi all. Casting problem here! If I have the following code: Type classType = typeof(MyDerivedClass); MyBaseClass o = Activator.CreateInstance(classType) as MyBaseClass; ... o.SomeFunctionInDerivedClass(); This all works fine and splendid. But if I have this code in a function, and I want to, at runtime, decide on the type of class being instantiated, how do I do the 'as' keyword explicit cast? I'm using 1.1 so can't use templates/whatever they are called. TIA Gizz

    S C 2 Replies Last reply
    0
    • G Gizz

      Hi all. Casting problem here! If I have the following code: Type classType = typeof(MyDerivedClass); MyBaseClass o = Activator.CreateInstance(classType) as MyBaseClass; ... o.SomeFunctionInDerivedClass(); This all works fine and splendid. But if I have this code in a function, and I want to, at runtime, decide on the type of class being instantiated, how do I do the 'as' keyword explicit cast? I'm using 1.1 so can't use templates/whatever they are called. TIA Gizz

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      If the type of the class is decided at runtime, then you can't have code like

      o.SomeFunctionInDerivedClass();

      either. What you'd have to do is use reflection to invoke methods, something like

      object obj = Activator.CreateInstance(passedType);
      obj.GetType().InvokeMember("SomeFunctionInDerivedClass",...);

      Of course, the type you instantiated might not have an "SomeFunctionInDerivedClass" method, it'll then result in an exception and you have to handle it. That's the price to pay for reflection, you lose compile time type safety. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      1 Reply Last reply
      0
      • G Gizz

        Hi all. Casting problem here! If I have the following code: Type classType = typeof(MyDerivedClass); MyBaseClass o = Activator.CreateInstance(classType) as MyBaseClass; ... o.SomeFunctionInDerivedClass(); This all works fine and splendid. But if I have this code in a function, and I want to, at runtime, decide on the type of class being instantiated, how do I do the 'as' keyword explicit cast? I'm using 1.1 so can't use templates/whatever they are called. TIA Gizz

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        You don't need the as MyBaseClass at all because objects that are derived from MyBaseClass build on that class. So

        MyBaseClass o = AnInstanceOfMyDerivedClass;

        is acceptable.


        My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

        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