C#对象types比较

我如何比较声明为types的两个对象的types。

我想知道两个对象是相同的types还是来自同一个基类。

任何帮助表示赞赏。

例如

private bool AreSame(Type a, Type b) { } 

ab是两个对象。 如果要查看ab是否在同一个inheritance层次结构中,则使用Type.IsAssignableFrom

 var t = a.GetType(); var u = b.GetType(); if (t.IsAssignableFrom(u) || u.IsAssignableFrom(t)) { // x.IsAssignableFrom(y) returns true if: // (1) x and y are the same type // (2) x and y are in the same inheritance hierarchy // (3) y is implemented by x // (4) y is a generic type parameter and one of its constraints is x } 

如果你想检查一个是否是另一个的基类,那么尝试Type.IsSubclassOf

如果您知道特定的基类,那么只需使用is关键字:

 if (a is T && b is T) { // Objects are both of type T. } 

否则,你将不得不直接走inheritance层次结构。

然而,这个想法有点问题,因为每个对象(事实上,每种types)都有一个公共基类Object。 你需要定义的是你想要inheritance的链条有多远(不pipe它们是相同的还是直接的父母,或者是直接的父母,等等)这样检查。 IsAssignableFrom对于确定types是否相互兼容是有用的,但是如果它们具有相同的父类(如果这就是你之后的),将不能完全确定。

如果你严格的标准是函数应该返回true,如果…

  • types是相同的
  • 一种是另一种的父母(直接或其他)
  • 这两种types具有相同的直接父母

你可以使用

 private bool AreSame(Type a, Type b) { if(a == b) return true; // Either both are null or they are the same type if(a == null || b == null) return false; if(a.IsSubclassOf(b) || b.IsSubclassOf(a)) return true; // One inherits from the other return a.BaseType == b.BaseType; // They have the same immediate parent } 

如果您希望两个对象实例具有某种types,您也可以使用“IS”关键字。 这也将用于比较子类与父类以及实现接口等的类。 虽然这不适用于typesType的types。

 if (objA Is string && objB Is string) // they are the same. public class a {} public class b : a {} b objb = new b(); if (objb Is a) // they are of the same via inheritance 

我尝试了使用接口和具体类的层次结构。 它遍历基类链中的一个types,直到它到达“对象”,我们检查当前的目标types是否可以分配给源types。 我们还检查这些types是否有共同的接口。 如果他们这样做,他们是“同”

希望这可以帮助。

  public interface IUser { int ID { get; set; } string Name { get; set; } } public class NetworkUser : IUser { public int ID { get; set; } public string Name { get; set; } } public class Associate : NetworkUser,IUser { #region IUser Members public int ID { get; set; } public string Name { get; set; } #endregion } public class Manager : NetworkUser,IUser { #region IUser Members public int ID { get; set; } public string Name { get; set; } #endregion } public class Program { public static bool AreSame(Type sourceType, Type destinationType) { if (sourceType == null || destinationType == null) { return false; } if (sourceType == destinationType) { return true; } //walk up the inheritance chain till we reach 'object' at which point check if //the current destination type is assignable from the source type Type tempDestinationType = destinationType; while (tempDestinationType.BaseType != typeof(object)) { tempDestinationType = tempDestinationType.BaseType; } if( tempDestinationType.IsAssignableFrom(sourceType)) { return true; } var query = from d in destinationType.GetInterfaces() join s in sourceType.GetInterfaces() on d.Name equals s.Name select s; //if the results of the query are not empty then we have a common interface , so return true if (query != Enumerable.Empty<Type>()) { return true; } return false; } public static void Main(string[] args) { AreSame(new Manager().GetType(), new Associate().GetType()); } }