Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Object Type Comparison

Tags:

c#

types

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

I want to know if two objects are of the same type or from the same base class.

Any help is appreciated.

e.g.

private bool AreSame(Type a, Type b) {  } 
like image 984
B Z Avatar asked Apr 02 '09 03:04

B Z


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

Say a and b are the two objects. If you want to see if a and b are in the same inheritance hierarchy, then 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 is a base class of the other, then try Type.IsSubclassOf.

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

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

Otherwise, you'll have to walk the inheritance hierarchy directly.

like image 196
John Feminella Avatar answered Sep 29 '22 23:09

John Feminella


There's a bit of a problem with this idea, though, as every object (and, indeed, every type) DOES have a common base class, Object. What you need to define is how far up the chain of inheritance you want to go (whether it's they're either the same or they have the same immediate parent, or one is the immediate parent of the other, etc.) and do your checks that way. IsAssignableFrom is useful for determining if types are compatible with one another, but won't fully establish if they have the same parent (if that's what you're after).

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

  • The types are identical
  • One type is the parent (immediate or otherwise) of the other
  • The two types have the same immediate parent

You could 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 } 
like image 26
Adam Robinson Avatar answered Sep 30 '22 01:09

Adam Robinson