C # object type comparison

How can I compare the types of two objects declared as a type.

I want to know if two objects have the same type or the same base class.

Any help is appreciated.

eg.

private bool AreSame(Type a, Type b) { } 
+56
c # types
Apr 02 '09 at 3:46
source share
4 answers

Say a and b are two objects. If you want to see if a and b in the same inheritance hierarchy, use 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 } 

If you want to check if one of the base classes is another, try Type.IsSubclassOf .

If you know a specific base class, just use the is keyword:

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

Otherwise, you will need to go directly through the inheritance hierarchy.

+84
Apr 02 '09 at 3:58
source share

There is a bit of a problem with this idea, since each object (and, indeed, each type) has a common base class Object. What you need to determine is how far you inherit the inheritance chain (regardless of whether they are the same or have the same immediate parent, or one of them is the direct parent of the other, etc.) And checks this way. IsAssignableFrom is useful for determining type compatibility with each other, but will not be fully set if they have the same parent (if that is what you need).

If your strict criteria are that the function should return true if ...

  • Types are identical
  • One type is the parent (immediate or another) of the other
  • Two types have the same immediate parent

you can use

 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 } 
+31
Apr 02 '09 at 4:28
source share

You can also use the "IS" keyword if you expect two instances of an object to have a specific type. This will also work to compare subclasses with parent classes, as well as classes that implement interfaces, etc. This will not work for types of type Type.

 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 
+13
Apr 02 '09 at 3:53
source share

I tried the following with a hierarchy using both interfaces and specific classes. It approaches the chain of the base class for one of the types until it reaches the "object" in which we check whether the current destination type is assigned to the source type. We also check if types have a common interface. if they do, then they are AreSame

Hope this helps.

  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()); } } 
+2
Apr 02 '09 at 5:24
source share



All Articles