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 the Data Type of a Generic

Determining the Data Type of a Generic

Scheduled Pinned Locked Moved C#
question
5 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.
  • K Offline
    K Offline
    kevinnicol
    wrote on last edited by
    #1

    I have a class, public class Collection<T> and I was wondering if there was a way to determine if the data type used implements an interface. I have used this code.

    if (typeof(T) == typeof(ICollectable))
    variance = 1.2;

    but it never gets into this if block, regardless if the class I use implements ICollectable. Any ideas?

    I K A P 4 Replies Last reply
    0
    • K kevinnicol

      I have a class, public class Collection<T> and I was wondering if there was a way to determine if the data type used implements an interface. I have used this code.

      if (typeof(T) == typeof(ICollectable))
      variance = 1.2;

      but it never gets into this if block, regardless if the class I use implements ICollectable. Any ideas?

      I Offline
      I Offline
      Ian Shlasko
      wrote on last edited by
      #2

      Sure... When you call typeof(T), it returns a Type object, which has plenty of useful methods like GetInterface(), FindInterface(), etc. Just don't use IsSubclassOf(), as that won't work for interfaces.

      Proud to have finally moved to the A-Ark. Which one are you in?
      Author of the Guardians Saga (Sci-Fi/Fantasy novels)

      1 Reply Last reply
      0
      • K kevinnicol

        I have a class, public class Collection<T> and I was wondering if there was a way to determine if the data type used implements an interface. I have used this code.

        if (typeof(T) == typeof(ICollectable))
        variance = 1.2;

        but it never gets into this if block, regardless if the class I use implements ICollectable. Any ideas?

        K Offline
        K Offline
        kevinnicol
        wrote on last edited by
        #3

        I've come up with this, which will work for now. I am unsure of it's efficency though, I assume it uses Reflection

        if (typeof(T).GetInterface("ICollectable") != null)

        1 Reply Last reply
        0
        • K kevinnicol

          I have a class, public class Collection<T> and I was wondering if there was a way to determine if the data type used implements an interface. I have used this code.

          if (typeof(T) == typeof(ICollectable))
          variance = 1.2;

          but it never gets into this if block, regardless if the class I use implements ICollectable. Any ideas?

          A Offline
          A Offline
          AspDotNetDev
          wrote on last edited by
          #4

          Reflection is probably the best way to go, but here is another technique which may be faster, though is not as flexible (i.e., you must know in advance if the type is a class or a struct, and it requires the class have a default constructor):

          public bool IsClassAnimal<T>()
          where T:new()
          {
          T t = new T();
          return t is IAnimal;
          }

          public bool IsStructAnimal<T>()
          {
          T t = default(T);
          if (t == null)
          {
          throw new InvalidOperationException("The type was a class.");
          }
          else
          {
          return t is IAnimal;
          }
          }

          Sample interface/struct/class:

          public interface IAnimal
          {
          }
          public class Dog : IAnimal
          {
          }
          struct Cat : IAnimal
          {
          }

          Sample usage:

          if (IsClassAnimal<Dog>())
          {
          MessageBox.Show("A dog is an animal!");
          }
          if (IsStructAnimal<Cat>())
          {
          MessageBox.Show("A cat is an animal!");
          }

          You could simplify that by just passing an instance of the class/struct, but that doesn't seem like what you're after. Also, if your class/struct is large (e.g., with lots of member variables), this could be a very expensive operation; not to mention it could have some weird side-effects. But, hey, thought I'd throw the technique out there anyway in case somebody can make use of it.

          [Forum Guidelines]

          1 Reply Last reply
          0
          • K kevinnicol

            I have a class, public class Collection<T> and I was wondering if there was a way to determine if the data type used implements an interface. I have used this code.

            if (typeof(T) == typeof(ICollectable))
            variance = 1.2;

            but it never gets into this if block, regardless if the class I use implements ICollectable. Any ideas?

            P Offline
            P Offline
            phil o
            wrote on last edited by
            #5

            What about :

            T myObj;

            if (myObj is ICollectable)
            variance = 1.2;

            ?

            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