locked
How to know type details at runtime - compile-time type vs. runtime type RRS feed

  • Question

  • Hi All,

    one of those simple questions, I didn't get an answer, yet.


    Is it possible to know the compile-time type of a variable at runtime, without using Generics ?

    Object.GetType() returns the actual type of the referenced object at runtime.

    It could be exactly the same type the variable was declared with, but it could also be any type that implements (or inherits from) the declared type.
    With a generic type parameter, the declared type can be 'extracted' with:    typeof(T).

    Do I really have to use Generics, just to get the declaring type of a variable ?



    Saturday, December 19, 2009 5:17 AM

Answers

  • Generics shouldn't be necessary unless your compile-time types are implicit (e.g., 'var').

    If you have a method, say:
      void MyMethod(IEnumerable<int> x)
    then:
      x.GetType()
    will return the actual runtime type of the object (e.g., List<int>), but:
      typeof(IEnumerable<int>)
    will return the declared type of x, i.e., IEnumerable<int>.

    If you're not fond of the syntax, or if you're dealing with complex, implicitly-declared compile-time types (e.g., using 'var'), then you can always define a helper method using generics:
      public static class Util { public static Type GetCompileTimeType<T>(this T obj) { return typeof(T); } }

          -Steve
    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ

    Microsoft Certified Professional Developer
    • Marked as answer by MWS muc.bav.de Saturday, December 19, 2009 10:38 PM
    • Unmarked as answer by MWS muc.bav.de Saturday, December 19, 2009 11:14 PM
    • Proposed as answer by Geert van Horrik Monday, December 21, 2009 8:35 AM
    • Marked as answer by MWS muc.bav.de Tuesday, December 22, 2009 12:27 AM
    Saturday, December 19, 2009 2:00 PM

All replies

  • If I understand correctly, you should be able to use reflection.

    Here is an intro:

    http://msdn.microsoft.com/en-us/library/ms173183.aspx

    And here are some details:

    http://msdn.microsoft.com/en-us/library/t0cs7xez.aspx

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Saturday, December 19, 2009 5:31 AM
  • Generics shouldn't be necessary unless your compile-time types are implicit (e.g., 'var').

    If you have a method, say:
      void MyMethod(IEnumerable<int> x)
    then:
      x.GetType()
    will return the actual runtime type of the object (e.g., List<int>), but:
      typeof(IEnumerable<int>)
    will return the declared type of x, i.e., IEnumerable<int>.

    If you're not fond of the syntax, or if you're dealing with complex, implicitly-declared compile-time types (e.g., using 'var'), then you can always define a helper method using generics:
      public static class Util { public static Type GetCompileTimeType<T>(this T obj) { return typeof(T); } }

          -Steve
    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ

    Microsoft Certified Professional Developer
    • Marked as answer by MWS muc.bav.de Saturday, December 19, 2009 10:38 PM
    • Unmarked as answer by MWS muc.bav.de Saturday, December 19, 2009 11:14 PM
    • Proposed as answer by Geert van Horrik Monday, December 21, 2009 8:35 AM
    • Marked as answer by MWS muc.bav.de Tuesday, December 22, 2009 12:27 AM
    Saturday, December 19, 2009 2:00 PM
  • Thanks for your replies,

    Stephen described the situation very clearly.

    But what I actually wanted to know, is if the static typeof, is in fact, the only way to get the compile-time type for any given varaibale, say passed in to a function.

    Like Stephen said, writing typof(T) is just another way to write the type name itself,
    for example, typof(<IEnumerable<int>).

    I wonder why there is no way to get the declared type with reflection.

    It is possible with generic types, but as far as I know, there is no way to know the compile-time type for non generic types.

    If anybody knows a way, please let me know.

    Thanks for your time.

    Tuesday, December 22, 2009 12:27 AM
  • There is a way to do this via reflection.

    The key is that the object referred to by the variable has nothing to do with the declared type of the variable. So, inspecting the object's type information is going down the wrong path.

    You should be able to inspect the method's type information to get the types of its paramters. Types of local variables are a lot harder; you'd have to partially reverse-engineer the MSIL code, I believe. In theory, however, it should be possible.

    I'm not a big fan of reflection, myself. :)  I much prefer generics...

          -Steve


    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ

    Microsoft Certified Professional Developer
    Tuesday, December 22, 2009 1:48 AM
  • Out of curiosity, what is it that you are trying to accomplish that requires that you know the declared type?

    Best regards,
    Johan Stenberg
    Microsoft Visual Studio
    Wednesday, December 23, 2009 12:06 AM