Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>EDIT</strong>: Update to reflect changes to handle interface types.<br> <strong>EDIT</strong>: Extra function added to support more than 2 types.</p> <p>First of all: A type cannot be an instance of another type (except when we are talking about an instance of type <code>Type</code>.) Only an object can be an instance of a type.</p> <p>Types can be subtypes of each other (using <code>IsSubType</code>). Types can be assignable to another type (using <code>IsAssignableFrom</code>).</p> <p>To find a common type, try this example. This will always produce a common type (object):</p> <pre><code> /// &lt;summary&gt; /// Returns the most common type of two types. /// If no common type can be found, null is returned. /// &lt;/summary&gt; static public Type GetCommonBaseClass(Type a, Type b) { if ((a == null) || (b ==null)) return null; if (a.IsInterface || b.IsInterface) return null; if (a.IsAssignableFrom(b)) return a; while (true) { if (b.IsAssignableFrom(a)) return b; b = b.BaseType; } } /// &lt;summary&gt; /// Returns the most common type of one or more types. /// If no common type can be found, null is returned. /// &lt;/summary&gt; static public Type GetCommonBaseClass(params Type[] types) { if ((types == null) || (types.Length == 0)) return null; Type type = types[0]; for (int i = 0; i &lt; types.Length; i++) type = GetCommonBaseClass(type, types[i]); return type; } </code></pre> <p>And called from your function:</p> <pre><code>public Type GetTestClass(Object left, Object right) { return GetCommonBaseClass(left.GetType(), right.GetType()); } </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. This table or related slice is empty.
    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