Note that there are some explanatory texts on larger screens.

plurals
  1. POGet The Class Name From an Object Variable
    text
    copied!<p>I would like to the get the class that is being pointed to from an Object variable.</p> <p>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?</p> <p>Example:</p> <pre><code>StringBuilder sbText = New StringBuilder(); Object oMyObject = sbText; // How can I determine that oMyObject points to an instance of StringBuilder object using only oMyObject </code></pre> <p>I have tried using <code>typeof(oMyObject)</code> or <code>oMyObject.GetType()</code> in every combination I can think of and still keep coming up with nothing more than <code>Object</code>. Seems like there should be a fairly straight forward way to do this and there probably is but I am not finding it.</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload