Get The Class Name From an Object Variable

dscarr

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.

Reed Copsey

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");
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Get object dynamically with the name stored in a variable

分類Dev

How to get object's class name by related_name in Django?

分類Dev

Get Class Name of Element from MouseEvent Target

分類Dev

Get a value from an object using a variable as a key

分類Dev

Get second level value from an object with a variable

分類Dev

How to get jquery object from $(.class)[0]

分類Dev

SCSS variable class name

分類Dev

SCSS variable class name

分類Dev

SCSS variable class name

分類Dev

Angular 6 + get elements with class name from document and change style

分類Dev

Jquery Get a dynamic class name from a dynamic field

分類Dev

How to get bucket name from Bucket object in AWS CDK for python

分類Dev

How to get a name value from a foreign key of a Json object

分類Dev

How can I get a variable from another class in Java?

分類Dev

Applying CSS on class with variable name

分類Dev

how to get first item from a group and prepare Data class object

分類Dev

Get attribute of an object from another class C++

分類Dev

Python class variable name vs __name__

分類Dev

Can class name and variable name be same in java?

分類Dev

How can I get the name of a property from inside an object to create a new property name?

分類Dev

Get ID via class name

分類Dev

JavaScript get Elements By Class Name

分類Dev

How would I create a class with a variable name in the class name?

分類Dev

Java - How do I get a variable generated from within a method in one class to another class?

分類Dev

Get an object property name as string

分類Dev

Using Part of a Class Name as a Variable for .addClass

分類Dev

Get Object type of class for ArrayList

分類Dev

Modify a member variable outside the class object and have changes in the class object

分類Dev

How to get address of function or global variable from its name in executable elf file for C?

Related 関連記事

  1. 1

    Get object dynamically with the name stored in a variable

  2. 2

    How to get object's class name by related_name in Django?

  3. 3

    Get Class Name of Element from MouseEvent Target

  4. 4

    Get a value from an object using a variable as a key

  5. 5

    Get second level value from an object with a variable

  6. 6

    How to get jquery object from $(.class)[0]

  7. 7

    SCSS variable class name

  8. 8

    SCSS variable class name

  9. 9

    SCSS variable class name

  10. 10

    Angular 6 + get elements with class name from document and change style

  11. 11

    Jquery Get a dynamic class name from a dynamic field

  12. 12

    How to get bucket name from Bucket object in AWS CDK for python

  13. 13

    How to get a name value from a foreign key of a Json object

  14. 14

    How can I get a variable from another class in Java?

  15. 15

    Applying CSS on class with variable name

  16. 16

    how to get first item from a group and prepare Data class object

  17. 17

    Get attribute of an object from another class C++

  18. 18

    Python class variable name vs __name__

  19. 19

    Can class name and variable name be same in java?

  20. 20

    How can I get the name of a property from inside an object to create a new property name?

  21. 21

    Get ID via class name

  22. 22

    JavaScript get Elements By Class Name

  23. 23

    How would I create a class with a variable name in the class name?

  24. 24

    Java - How do I get a variable generated from within a method in one class to another class?

  25. 25

    Get an object property name as string

  26. 26

    Using Part of a Class Name as a Variable for .addClass

  27. 27

    Get Object type of class for ArrayList

  28. 28

    Modify a member variable outside the class object and have changes in the class object

  29. 29

    How to get address of function or global variable from its name in executable elf file for C?

ホットタグ

アーカイブ