Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>leftClass.IsInstanceOfType(right)</code> means that "the right is a left". <code>rightClass.IsInstanceOfType(left)</code> means that "the left is a right".</p> <p>If the right is a left, but the left is not a right, then the left must be the super class, and the right must be the subclass. This is equivalent to the first case, where you have the comment "rightClass is a subclass of leftClass". So, it seems like the comments' intentions are actually accurate.</p> <p>However, I see a few problems with the method. The comments are the least of your worries.</p> <ol> <li><p>It will throw <code>NullReferenceException</code> if either parameter is null.</p></li> <li><p>It makes unnecessary calls to <code>GetType()</code>, because the actual source code for <code>IsInstanceOfType</code> looks like this:</p> <pre><code>public virtual bool IsInstanceOfType(object o) { if (o == null) { return false; } return this.IsAssignableFrom(o.GetType()); } </code></pre> <p>You should take @p.s.w.g.'s advice and use <code>IsAssignableFrom</code>, and possibly consider refactoring the signature compare two Types instead of two Objects.</p></li> <li><p>Any two concrete types will always have at least a common base type of <code>System.Object</code>. Returning null is not an acceptable result here.</p></li> <li><p>It doesn't handle cases where one type is not linearly derived from the other type, but both still have a common base class that is more derived than <code>System.Object</code>. For example,</p> <pre><code>public class Base { } public class A : Base { } public class B : Base { } </code></pre> <p>Your method would say that <code>A</code> and <code>B</code> are unrelated and return null, where the correct "common base" would be <code>Base</code>.</p></li> </ol> <p>I would look at the implementation provided at <a href="https://stackoverflow.com/questions/353430/easiest-way-to-get-a-common-base-class-from-a-collection-of-types">Easiest way to get a common base class from a collection of types</a>, which still is not perfect, but is better than the one you posted.</p> <h2>Update : Working code</h2> <p>I decided not to be stingy and I've posted the method that I use for this purpose below. Good luck.</p> <pre><code> /// &lt;summary&gt; Finds the most derived common base class of all the provided types, or System.Object if there is no common base class &lt;/summary&gt; public static Type CommonBaseClass(params Type[] types) { if(ReferenceEquals(types,null)) return typeof(object); types = types.Where(x =&gt; !ReferenceEquals(x,null)).Distinct().ToArray(); switch (types.Length) { case 0: return typeof(object); case 1: return types[0].IsInterface ? typeof(object): types[0]; default: IEnumerable&lt;IEnumerable&lt;Type&gt;&gt; hierarchies = types.Select(ClassHierarchy).OrderBy(x =&gt; x.Count()); Queue&lt;Type&gt; smallest = new Queue&lt;Type&gt;(hierarchies.First().Reverse()); hierarchies = hierarchies.Skip(1); do { int maxPossible = smallest.Count; hierarchies = hierarchies.Select(each =&gt; each.Take(maxPossible)); Type candidate = smallest.Dequeue(); if (hierarchies.All(each =&gt; each.Last() == candidate)) return candidate; } while (smallest.Count &gt; 1); return typeof(object); } } ///&lt;summary&gt;Gets the class hierarchy of the provided type, in order of derivation, e.g. : (System.Object,CustomBaseType,CustomConcreteType,...), or the singleton of System.Object type if the provided type is an interface or null &lt;/summary&gt; public static IEnumerable&lt;Type&gt; ClassHierarchy(this Type type) { if (type == null || type.IsInterface) type = typeof(object); var stack = new Stack&lt;Type&gt;(); do { stack.Push(type); type = type.BaseType; } while (type != null); return stack; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload