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. Cast to a Generic Type at runtime

Cast to a Generic Type at runtime

Scheduled Pinned Locked Moved C#
helpquestion
1 Posts 1 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.
  • F Offline
    F Offline
    Florian Storck
    wrote on last edited by
    #1

    Hi, I have the following problem: I have a generic, which derives from a normal base class like this: public class MyClass : BaseClass { T m_Value; public SetValue(T val) { // ... } public T GetValue(){ ... } ; } These classes are stored in a generic dictionary, which is instantiated like this: dictionary dic = new dictionary(); At one point, I want to acess the SetValue Function, but I have to cast to the exact type, and typeof() / GetType doesn't works here. What I try to do looks similar like this, but I haven't a solution yet; MyClass< dic["Key"].GetType() > tC, tC2; tC.SetValue((dic["Key"].GetType())dic["Key"].GetValue()); This isn't correct (i left out the instantiation) , I know, but I need to cast at runtime to the exact generic type, stored at the position in the dictionary, for accessing the generic, type specific Get/Set function. Any comments ? Thanks, Florian

    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