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. variable as type of generic (method)

variable as type of generic (method)

Scheduled Pinned Locked Moved C#
questionhelplearning
4 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.
  • S Offline
    S Offline
    Seishin
    wrote on last edited by
    #1

    Hi! so i've got this generic method: UserControl _control; public void InitializeSomething(WorkItem workItem) where Tclass : UserControl { _control = workItem.Items.AddNew(); } my goal is to call this method sth like this: Type someType = typeof(someOtherObject); someObj.InitializeSomething(WorkItem); but this of course won't work.. my question is: is it possible to pass variable containing some type as Tclass for generic?! thanks for any help!!!

    life is study!!!

    E 1 Reply Last reply
    0
    • S Seishin

      Hi! so i've got this generic method: UserControl _control; public void InitializeSomething(WorkItem workItem) where Tclass : UserControl { _control = workItem.Items.AddNew(); } my goal is to call this method sth like this: Type someType = typeof(someOtherObject); someObj.InitializeSomething(WorkItem); but this of course won't work.. my question is: is it possible to pass variable containing some type as Tclass for generic?! thanks for any help!!!

      life is study!!!

      E Offline
      E Offline
      Eduard Keilholz
      wrote on last edited by
      #2

      I don't really get what you're aiming for... Doe you want to create a collection of controls which are derived from some kind of type?

      .: I love it when a plan comes together :. http://www.zonderpunt.nl

      S 1 Reply Last reply
      0
      • E Eduard Keilholz

        I don't really get what you're aiming for... Doe you want to create a collection of controls which are derived from some kind of type?

        .: I love it when a plan comes together :. http://www.zonderpunt.nl

        S Offline
        S Offline
        Seishin
        wrote on last edited by
        #3

        mmh.. well, for example List<>.. inside the <> you have to put class name.. and my aim is to pass this class name through some variable.

        life is study!!!

        G 1 Reply Last reply
        0
        • S Seishin

          mmh.. well, for example List<>.. inside the <> you have to put class name.. and my aim is to pass this class name through some variable.

          life is study!!!

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          You can't specify the type for generics with a variable. The point with generics is that the type is known at compile time so that the compiler can generate the code for the created generic type.

          --- single minded; short sighted; long gone;

          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