Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'll have a default implementation and some well-known classes and interface sorted by priority to have in mind. Here my implementation:</p> <pre><code>private static List&lt;Type&gt; CommonTypesPriorities = new List&lt;Type&gt; { typeof(IEnumerable), typeof(Array), typeof(IClonable) }; public static Type FindAssignableWith(this Type type1, Type type2) { if(type1 == type2) return type1; var baseClass = type1.FindBaseClassWith(type2); //if the base class is not object/null and it is not in the list, then return it. if(baseClass != typeof(object) &amp;&amp; baseClass != null &amp;&amp; !CommonTypesPriorities.Contains(type)) return baseClass; var @interface = type1.FindInterfaceWith(type2); if(@interface == null) return baseClase; //if there's no base class and the found interface is not in the list, return it if(baseClass != null &amp;&amp; !CommonTypesPriorities.Contains(@interface) return @interface; //Now we have some class and interfaces from the list. Type type = null; int currentPriority; //if the base class is in the list, then use it as the first choice if(baseClass != null &amp;&amp; CommonTypesPriorities.Contains(type)) { type = baseClass; currentPriority = CommonTypesPriorities.IndexOf(type); } var interfaces1 = type1.GetInterfaces(); var interfaces2 = type2.GetInterfaces(); foreach(var i in interfaces1) { if(interfaces2.Contains(i)) { //We found a common interface. Let's check if it has more priority than the current one var priority = CommonTypesPriorities.IndexOf(i); if(i &gt;= 0 &amp;&amp; i &lt; currentPriority) { currentPriority = priority; type = i; } } } return type; </code></pre> <p>}</p> <p>Hope it helps.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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