Home > OS >  How do I find the class of the object that's assigned to a variable?
How do I find the class of the object that's assigned to a variable?

Time:05-24

I have some code like this, where I don't know what class was assigned to someObject:

public ISomeInterface someObject;

public void DoThing()
{
    someObject.DoAnotherThing();
}

How can I find out what's the class of the object assigned to someObject? Is there a button in Visual Studio to show this? When I hover over someObject inside of DoThing, Visual Studio gives me a list of properties and their values on someObject, but it doesn't tell me what the class of someObject is.

CodePudding user response:

You can also do the same thing while you're debugging. If you're stopped at a break point you can enter that expression in the Watch window below.

enter image description here

The type of the variable will show up in the Type column in braces, and you have to expand the Watch window very wide to see it. The Type column will look like this:

SomeNamespace.ISomeInterface {SomeNamespace.SomeClass}

CodePudding user response:

I figured it out while writing the question. There isn't a way to see this in Visual Studio directly as far as I can tell (if someone figures it out please post another answer), but we can do it within C#:

System.Type type = someObject.GetType();
string typeFullName = type.FullName;
  • Related