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. Determining a generic type at runtime

Determining a generic type at runtime

Scheduled Pinned Locked Moved C#
questioncomalgorithmstoolshelp
3 Posts 2 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.
  • I Offline
    I Offline
    Ista
    wrote on last edited by
    #1

    I have a class public class myclass : BindingList<T> {} I have created(shadowed) the GetEnumerator method to provide some custom sorting and what not. But the problem is the type can be Category, Topic, or Entry. And I need to test each one because each has its own sorting strategy. I tried if( typeof(T) is Category) but that doesn't work. What is the easiest way to determine the type T is?

    -------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog

    L 1 Reply Last reply
    0
    • I Ista

      I have a class public class myclass : BindingList<T> {} I have created(shadowed) the GetEnumerator method to provide some custom sorting and what not. But the problem is the type can be Category, Topic, or Entry. And I need to test each one because each has its own sorting strategy. I tried if( typeof(T) is Category) but that doesn't work. What is the easiest way to determine the type T is?

      -------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog

      L Offline
      L Offline
      LongRange Shooter
      wrote on last edited by
      #2

      typeof(T) will always return a type of T, and T is really a type identifier. What you may try is this:

      public void Sort(T item)
      {
      if ( item is Category )...
      if ( item is Volume ) ...
      }

      I 1 Reply Last reply
      0
      • L LongRange Shooter

        typeof(T) will always return a type of T, and T is really a type identifier. What you may try is this:

        public void Sort(T item)
        {
        if ( item is Category )...
        if ( item is Volume ) ...
        }

        I Offline
        I Offline
        Ista
        wrote on last edited by
        #3

        lol. its a GetEnumerator(). Theres no definition like that. Type t = typeof(T); if( t.Equals(Category) ) I figured it out just a second ago Thanks tho

        -------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog

        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