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. dynamic casting

dynamic casting

Scheduled Pinned Locked Moved C#
question
4 Posts 4 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.
  • M Offline
    M Offline
    matt cole
    wrote on last edited by
    #1

    Hi, I have an object "obj" that is a TypeA. I have a Type object that refers to TypeA. How can I cast obj to TypeA? TypeA is dynamically created from a loaded assembly, so I need to be able to do it dynamically. Or altenatively is there another way to get at obj's fields defined by the TypeA interface ? thanks, Matt

    L M H 3 Replies Last reply
    0
    • M matt cole

      Hi, I have an object "obj" that is a TypeA. I have a Type object that refers to TypeA. How can I cast obj to TypeA? TypeA is dynamically created from a loaded assembly, so I need to be able to do it dynamically. Or altenatively is there another way to get at obj's fields defined by the TypeA interface ? thanks, Matt

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      You gonna have to explain better, perhaps a code segment. Dynamic casting is an urban legend, and is never required! (yes it does feel like u need it sometimes, but its not necessary, really) top secret xacc-ide 0.0.1

      1 Reply Last reply
      0
      • M matt cole

        Hi, I have an object "obj" that is a TypeA. I have a Type object that refers to TypeA. How can I cast obj to TypeA? TypeA is dynamically created from a loaded assembly, so I need to be able to do it dynamically. Or altenatively is there another way to get at obj's fields defined by the TypeA interface ? thanks, Matt

        M Offline
        M Offline
        Mardawi
        wrote on last edited by
        #3

        maybe typeof() helps. it will give the type of the object. you should have cases of what types could obj be, it can't be just anything. unless you should go for Generics in C# V2.0 find about generics here: http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=370

        1 Reply Last reply
        0
        • M matt cole

          Hi, I have an object "obj" that is a TypeA. I have a Type object that refers to TypeA. How can I cast obj to TypeA? TypeA is dynamically created from a loaded assembly, so I need to be able to do it dynamically. Or altenatively is there another way to get at obj's fields defined by the TypeA interface ? thanks, Matt

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Not possible. Because of the Common Type System (CTS) - part of the .NET Framework - the type must be known at compile time or the necessary IL instructions cannot be emitted as part of the module that gets embedded into the primary assembly. The best alternative is to develop a base class from which all of your loaded types (common, especially in plug-in style applications) and cast to that base class, relying on polymorphism to define your distinct types' functionality (that derive from that base class, of course).

          Microsoft MVP, Visual C# My Articles

          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