简体   繁体   English

C#确定对象的类型

[英]C# Determining the type of an object

I'm not exactly sure how to ask this question well. 我不确定如何很好地问这个问题。 Sorry if it's unclear. 很抱歉,如果不清楚。 I'm trying to determine the type an object is acting as, not the actual type of an object. 我正在尝试确定对象充当的类型,而不是对象的实际类型。

Say I have these two classes: 说我有这两节课:

public class A{
    public A() {
        string thisTypeName = this.GetType().???; // the value I'm looking for here is "A" not "B"
    }
}

public class B : A { }

What I do not want is... 我不想要的是...

string thisTypeName = this.GetType().BaseType.Name;

...because if I decide to do this... ...因为我决定这样做...

public class C : B { }

...then now my this.GetType().BaseType will reference the class B, not A. ...然后我的this.GetType()。BaseType将引用类B,而不是类A。

Is there a built-in way to do this? 有内置的方法吗? I'm guessing I can probably write an extension method on Type that will do what I want. 我猜想我可能可以在Type上编写一个扩展方法,该方法可以实现我想要的功能。

Watch it with some of the advice you are getting now. 收看其中的一些建议,然后观看。 Some of that is kind of smelly in my book. 其中有些在我的书中有点臭。

You want to use: 您要使用:

typeof(A).IsAssigneableFrom(aninstance.GetType());

to determine whether a type is derived from a basetype, or implements an interface 确定类型是从基类型派生还是实现接口

See: http://msdn.microsoft.com/en-us/library/system.type.isassignablefrom.aspx . 请参阅: http : //msdn.microsoft.com/zh-cn/library/system.type.isassignablefrom.aspx This page has a slew of interesting examples showing all the subtle cases it supports 此页面上有许多有趣的示例,显示了它支持的所有微妙情况

    public static Type GetActualBaseType(this object obj)
    {
        Type type = obj.GetType();

        while (type.IsNested)
            type = type.BaseType;

        return type;
    }

you could attempt to use that 你可以尝试使用

You could use System.Diagnostics.StackTrace to get the stack frame, and then get the DeclaringType for the current method: 您可以使用System.Diagnostics.StackTrace获取堆栈框架,然后获取当前方法的DeclaringType

var st = new StackTrace();
Console.WriteLine(st.GetFrame(0).GetMethod().DeclaringType.ToString());

what you seem to be looking for is known at compile time: 您似乎在寻找什么在编译时就知道了:

in ctor of B you want the type B 在B的ctor中,您想要B型

in ctor of A you want the type A ... 在A的ctor中,您想要A类型...

so you could simply write typeof(A) , typeof(B) , etc 所以你可以简单地写出typeof(A)typeof(B)

if you want to get the type by reflection that declared the current method: 如果要通过反射获取声明当前方法的类型:

MethodBase.GetCurrentMethod().DeclaringType

Do you really need a type string of a given class instance, or do you want to know how to handle different child types of a certain base class? 您是否真的需要给定类实例的类型字符串,或者您想知道如何处理某个基类的不同子类型?

In the latter case, it's probably better to use the is keyword, like this: 在后一种情况下,最好使用is关键字,如下所示:

class A {}
class B : A {}
class C : B {}
class Aaa : A {}

// NB: order of if() statements is important below, start with most specific ones
public void doSomething(A obj) {
  if(obj is Aaa) {
    // do something specific for Aaa
  } else if(obj is C) {
    // do something specific for C
  } else if(obj is B) {
    // do something specific for B
  } else {
    // do general A stuff
  }
}

I have no idea if this is where you were trying to go, but from the question you posed and your comment it's clear that just getting the name of the base class is not the real problem. 我不知道这是否是您要去的地方,但是从您提出的问题和您的评论来看,很明显,仅获取基类的名称并不是真正的问题。

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

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