How to determine type of a variable? (not the type of the object)
Asked Answered
P

5

8

I'm in the immediate window in Visual Studio. There's a variable p. How can I deduce the type of the variable p?

I tried p.GetType() but that returns the type of object p. In my case, this is a very specific type (eg. sometimes ChessPlayer, sometimes TennisPlayer). I'd like to know the type of the variable, ie. the type that determines what methods are available on the variable p.


Edit: I think this is a reasonable thing to want to do. I am trying to inspect the variable p but I don't know it is! Normally in Visual Studio I just hover the mouse on the variable and it tells me its type, or I type . and the autocomplete lists its methods. However none of that works in the immediate window, all I have is this variable p I don't know what it is or what I can do with it :(

Paresh answered 21/11, 2012 at 11:5 Comment(4)
Have you tried p.GetType().GetMethods()? p.GetType().GetInterfaces()?Daffy
Although I am curious as to whether there is a way to do this, I wouldn't be surprised were there not. If you're in the debugger with a handle on p, you can see the declaration of p in the code and thus see what its "variable" type is.Halette
Just to clarify, when doing BoardGamePlayer p = new ChessPlayer();, what you want is a way to know that p was declared as a BoardGamePlayer?Gelinas
To satisfy my curiosity, why would you want that? The information you're seeking is, by definition, known at compile time. What is the point in trying to retrieve it dynamically? Whatever you're really trying to do, there's probably an easier way.Gelinas
P
4

Surprised this was so difficult, in the end I wrote this method, which seems to give the right answer.

public static class Extensions
{
    public static Type GetVariableType<T>(this T instance)
    {
        return typeof(T);
    }
}

Example usage:

void Main()
{
    IList x = new List<int>{};
    x.GetVariableType().Dump();
}

Prints System.Collections.IList

Paresh answered 21/11, 2012 at 12:16 Comment(0)
W
6

c# provides many ways for this :)

For the exact copy of specific type you need to do this

if (p.GetType() == typeof(YourDesiredType))

If you want to know whether p is an instance of yourdesiredtype then

if (p is YourDesiredType)

or you can try this

YourDesiredType ydp = p as YourDesiredType;

As in this case (as i'm not sure that it is possible in your scenario)when OP wants to know the compile type Then I would only recommend to use a generic list for this

Because by keeping Type safe list , everyone can easily keep track for its type

Walke answered 21/11, 2012 at 11:11 Comment(2)
This is all checking the runtime type. The OP is interested in the compile-time type.Halette
He just cleared that.I think There was no need for the down vote :( that someone didWalke
P
4

Surprised this was so difficult, in the end I wrote this method, which seems to give the right answer.

public static class Extensions
{
    public static Type GetVariableType<T>(this T instance)
    {
        return typeof(T);
    }
}

Example usage:

void Main()
{
    IList x = new List<int>{};
    x.GetVariableType().Dump();
}

Prints System.Collections.IList

Paresh answered 21/11, 2012 at 12:16 Comment(0)
L
1

As an alternative to using the immediate window, if you only want to see the type of the variable, you can simply add a variable watch and check the type in the watch window.

Louanne answered 3/4, 2017 at 5:46 Comment(0)
K
0
System.Object.GetType()

This will return you the type of the variable because this class in the top of hierarchy from which every class is derived.

you can also check typeof function to get the exact type of the given instance.

Kilohertz answered 21/11, 2012 at 11:11 Comment(2)
The OP says he's tried GetType. He wants the compile-type type of p, not the run-time type.Halette
well, then typeof would be possible solution for this.Kilohertz
M
0

I think you might need this

if (p is ChessPlayer)
{
    ChessPlayer cp = (ChessPlayer)p;

    //Use ChessPlayer methods
}
Monocycle answered 21/11, 2012 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.