dscarr
dscarr

Reputation: 1830

Get The Class Name From an Object Variable

I would like to the get the class that is being pointed to from an Object variable.

For instance, if I have an instance of a StringBuilder object that I subsequently set an Object variable to, can I somehow know that the Object variable points to a StringBuilder object?

Example:

StringBuilder sbText = New StringBuilder();
Object oMyObject = sbText;
// How can I determine that oMyObject points to an instance of StringBuilder object using only oMyObject

I have tried using typeof(oMyObject) or oMyObject.GetType() in every combination I can think of and still keep coming up with nothing more than Object. Seems like there should be a fairly straight forward way to do this and there probably is but I am not finding it.

I must disagree with the user who marked this as a duplicate question to the one they provided the link to. The title of my question may not have been as clear as it could have been (I have altered it a bit now) and the answers to both may involve the same method but the user who asked the other question was looking for a way to instantiate an object as the same type as another object. I was only looking for the way to get the name of a class when all I have is a variable of type Object. I would never have come up with the answer that Reed provided by looking at that question and I don't recall it ever coming up in a search on this site or a wider Google search.

Upvotes: 11

Views: 30859

Answers (5)

Obsidian Phoenix
Obsidian Phoenix

Reputation: 4155

All of these will work:

StringBuilder sbText = new StringBuilder();
Object oMyObject = sbText;

oMyObject is StringBuilder;
oMyObject.GetType() == typeof(StringBuilder);

StringBuilder test = oMyObject as StringBuilder;
if (test != null)
{
  //Do Work
}

Upvotes: 0

Ehsan
Ehsan

Reputation: 32721

How can I determine that oMyObject points to an instance of StringBuilder object using only oMyObject

you can do this

StringBuilder sbText = new StringBuilder();
Object oMyObject = sbText;
if (oMyObject is StringBuilder)
{
   StringBuilder sb = (StringBuilder)oMyObject; // safe now as you know type
   //your code
}

However if you need to get the type you need to use GetType on object.

Upvotes: 0

Kevin DiTraglia
Kevin DiTraglia

Reputation: 26078

Probably because you are storing the Type into an object. Try this:

Type myType = sbText.GetType();

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564831

GetType() should provide the proper System.Type of the object at runtime.

For example, this prints "StringBuilder":

StringBuilder sbText = new StringBuilder();
Object oMyObject = sbText;

Console.WriteLine(oMyObject.GetType().Name);

Note that if you just want to check for a specific class type, is (or as) often works more cleanly than getting a Type:

StringBuilder sbText = new StringBuilder();
Object oMyObject = sbText;

//...

StringBuilder sb = oMyObject as StringBuilder;
if (sb != null)
{
    // oMyObject was a StringBuilder - you can use sb as needed:
    sb.AppendText("Foo");
}

Upvotes: 24

JeremiahDotNet
JeremiahDotNet

Reputation: 910

if (oMyObject is StringBuilder) { ... }

Upvotes: -1

Related Questions