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. Assigning Generic Type during runtime

Assigning Generic Type during runtime

Scheduled Pinned Locked Moved C#
designhelpquestion
6 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.
  • 2 Offline
    2 Offline
    2hdass
    wrote on last edited by
    #1

    In the code I am working with i have a class called RecordHistoryService<x,y> X, and Y are unknown at design time and known at runtime i need to instantiate RecordHistoryService<x,y>, is there any way to do it? I have this, but it doesnt compile: i.e.

    Type _rhType = Type.GetType(rhType);
    Type _entityType = Type.GetType(entityType);
    RecordHistoryService<_rhType, _entityType> recordHistory;

    thx for your help :D

    E C M 3 Replies Last reply
    0
    • 2 2hdass

      In the code I am working with i have a class called RecordHistoryService<x,y> X, and Y are unknown at design time and known at runtime i need to instantiate RecordHistoryService<x,y>, is there any way to do it? I have this, but it doesnt compile: i.e.

      Type _rhType = Type.GetType(rhType);
      Type _entityType = Type.GetType(entityType);
      RecordHistoryService<_rhType, _entityType> recordHistory;

      thx for your help :D

      E Offline
      E Offline
      Expert Coming
      wrote on last edited by
      #2

      Look into reflection.

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

      2 1 Reply Last reply
      0
      • E Expert Coming

        Look into reflection.

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

        2 Offline
        2 Offline
        2hdass
        wrote on last edited by
        #3

        Is that the only way to do this? I know that reflection is rather expensive.

        E 1 Reply Last reply
        0
        • 2 2hdass

          In the code I am working with i have a class called RecordHistoryService<x,y> X, and Y are unknown at design time and known at runtime i need to instantiate RecordHistoryService<x,y>, is there any way to do it? I have this, but it doesnt compile: i.e.

          Type _rhType = Type.GetType(rhType);
          Type _entityType = Type.GetType(entityType);
          RecordHistoryService<_rhType, _entityType> recordHistory;

          thx for your help :D

          C Offline
          C Offline
          Caio Kinzel Filho
          wrote on last edited by
          #4

          If the types are not totally random, you could use some kind of factory method to instantiate it...if totally random, I think the easiest way is to use reflection, or something like this article

          Intelligence is almost useless for those who have nothing else! Email: caiokf@gmail.com

          1 Reply Last reply
          0
          • 2 2hdass

            Is that the only way to do this? I know that reflection is rather expensive.

            E Offline
            E Offline
            Expert Coming
            wrote on last edited by
            #5

            It is the only way I can think of doing it. It is kind of expensive but not too horrible, one alternative(and I say that for lack of better word) is to make a bunch of methods that are specific to the type being passed, although I would NEVER do this, it is horrible practice. My suggestion is reflection, although kind of expensive, it won't be bad unless doing this many times very fast.

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

            1 Reply Last reply
            0
            • 2 2hdass

              In the code I am working with i have a class called RecordHistoryService<x,y> X, and Y are unknown at design time and known at runtime i need to instantiate RecordHistoryService<x,y>, is there any way to do it? I have this, but it doesnt compile: i.e.

              Type _rhType = Type.GetType(rhType);
              Type _entityType = Type.GetType(entityType);
              RecordHistoryService<_rhType, _entityType> recordHistory;

              thx for your help :D

              M Offline
              M Offline
              Mark Churchill
              wrote on last edited by
              #6

              You'd be looking at Type.MakeGenericType(...) off the top of my head.

              Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
              Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

              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