Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to find the smallest assignable type in two types (duplicate)?
    primarykey
    data
    text
    <p>Here're two extension methods for use</p> <pre><code>public static Type FindInterfaceWith(this Type type1, Type type2) { // returns most suitable common implemented interface } public static Type FindBaseClassWith(this Type type1, Type type2) { // returns most derivative of common base class } </code></pre> <ul> <li><code>FindInterfaceWith</code> returns <code>null</code> if they don't have common implemented interface. <br/></li> <li><code>FindBaseClassWith</code> returns <code>System.Object</code> if they have no more derivative common base class. </li> <li><code>FindBaseClassWith</code> returns <code>null</code> if one of parameters was an interface. </li> <li>Both they return <code>null</code> if any of parameter was <code>null</code>. </li> </ul> <p>And the signature of method in finally solution would be like:</p> <pre><code>public static Type FindAssignableWith(this Type type1, Type type2) { // what should be here? } </code></pre> <p>Reflection and Linq are restricted to use, except there are no other way.</p> <p>Are there good ways to find the best fit of common type between <code>type1</code> and <code>type2</code>? </p> <p>Or are there something better to achieve this? </p> <hr> <p><strong>update:</strong> </p> <p>By my personal understanding, because of the <strong><em>ability to implement multiple interfaces</em></strong> with a class, the <code>FindInterfaceWith</code> could possibly need to call <code>FindBaseClassWith</code> internally; otherwise the best choice of type would be undecidable. </p> <p>If this supposition was correct, then the <code>FindInterfaceWith</code> becomes a redundant method; because of the only difference between <code>FindInterfaceWith</code> and <code>FindAssignableWith</code> is: </p> <p><strong><em><code>FindInterfaceWith</code> returns <code>null</code> if there was a best choice of class; while <code>FindAssignableWith</code> returns the exact class directly.</em></strong> </p> <p>Otherwise, they both return a best choice of interface.</p> <p>This is about saying the original assumption was irrational. That is, <strong><em><code>FindInterfaceWith</code> cannot be implemented if <code>FindAssignableWith</code> is not</em></strong>. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    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