简体   繁体   English

从对象变量获取类名

[英]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. 我想从Object变量获取指向的类。

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? 例如,如果我有一个StringBuilder对象的实例,随后又将Object变量设置为该实例,我是否可以某种方式知道Object变量指向StringBuilder对象?

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 . 我已经尝试过在我能想到的每种组合中使用typeof(oMyObject)oMyObject.GetType() ,并且仍然不断提出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. 当我仅有的是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. 通过查看该问题,我永远都不会想到里德提供的答案,而且我也不会记得在此网站上进行的搜索或更广泛的Google搜索中所给出的答案。

GetType() should provide the proper System.Type of the object at runtime. GetType()应该在运行时提供正确的对象的System.Type

For example, this prints "StringBuilder" : 例如,这将显示"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 : 请注意,如果您只想检查特定的类类型, is (或as )通常比获取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");
}
if (oMyObject is StringBuilder) { ... }

Probably because you are storing the Type into an object. 可能是因为您将Type存储到对象中。 Try this: 尝试这个:

Type myType = sbText.GetType();

How can I determine that oMyObject points to an instance of StringBuilder object using only oMyObject 如何仅使用oMyObject确定oMyObject指向StringBuilder对象的实例

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. 但是,如果需要获取类型,则需要在对象上使用GetType

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM