Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9068321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:09:50+00:00 2026-06-16T17:09:50+00:00

Here’re two extension methods for use public static Type FindInterfaceWith(this Type type1, Type type2)

  • 0

Here’re two extension methods for use

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
}
  • FindInterfaceWith returns null if they don’t have common implemented interface.
  • FindBaseClassWith returns System.Object if they have no more derivative common base class.
  • FindBaseClassWith returns null if one of parameters was an interface.
  • Both they return null if any of parameter was null.

And the signature of method in finally solution would be like:

public static Type FindAssignableWith(this Type type1, Type type2) {
    // what should be here?
}

Reflection and Linq are restricted to use, except there are no other way.

Are there good ways to find the best fit of common type between type1 and type2?

Or are there something better to achieve this?


update:

By my personal understanding, because of the ability to implement multiple interfaces with a class, the FindInterfaceWith could possibly need to call FindBaseClassWith internally; otherwise the best choice of type would be undecidable.

If this supposition was correct, then the FindInterfaceWith becomes a redundant method; because of the only difference between FindInterfaceWith and FindAssignableWith is:

FindInterfaceWith returns null if there was a best choice of class; while FindAssignableWith returns the exact class directly.

Otherwise, they both return a best choice of interface.

This is about saying the original assumption was irrational. That is, FindInterfaceWith cannot be implemented if FindAssignableWith is not.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:09:51+00:00Added an answer on June 16, 2026 at 5:09 pm

    Here is my implementation:

    FindAssignableWith, FindBaseClassWith and FindInterfaceWith implementations

    // provide common base class or implemented interface
    public static Type FindAssignableWith(this Type typeLeft, Type typeRight)
    {
        if(typeLeft == null || typeRight == null) return null;
    
        var commonBaseClass = typeLeft.FindBaseClassWith(typeRight) ?? typeof(object);
    
        return commonBaseClass.Equals(typeof(object))
                ? typeLeft.FindInterfaceWith(typeRight)
                : commonBaseClass;
    }
    
    // searching for common base class (either concrete or abstract)
    public static Type FindBaseClassWith(this Type typeLeft, Type typeRight)
    {
        if(typeLeft == null || typeRight == null) return null;
    
        return typeLeft
                .GetClassHierarchy()
                .Intersect(typeRight.GetClassHierarchy())
                .FirstOrDefault(type => !type.IsInterface);
    }
    
    // searching for common implemented interface
    // it's possible for one class to implement multiple interfaces, 
    // in this case return first common based interface
    public static Type FindInterfaceWith(this Type typeLeft, Type typeRight)
    {
        if(typeLeft == null || typeRight == null) return null;
    
        return typeLeft
                .GetInterfaceHierarchy()
                .Intersect(typeRight.GetInterfaceHierarchy())
                .FirstOrDefault();   
    }
    
    // iterate on interface hierarhy
    public static IEnumerable<Type> GetInterfaceHierarchy(this Type type)
    {
        if(type.IsInterface) return new [] { type }.AsEnumerable();
    
        return type
                .GetInterfaces()
                .OrderByDescending(current => current.GetInterfaces().Count())
                .AsEnumerable();
    }
    
    // interate on class hierarhy
    public static IEnumerable<Type> GetClassHierarchy(this Type type)
    {
        if(type == null) yield break;
    
        Type typeInHierarchy = type;
    
        do
        {
            yield return typeInHierarchy;
            typeInHierarchy = typeInHierarchy.BaseType;
        }
        while(typeInHierarchy != null && !typeInHierarchy.IsInterface);
    }
    

    Remark regarding FindInterfaceWith implementation

    Any interfaces that implements either IEnumerable or IEnumerable<T> will be selected before others, what I considered not to be correct

    Open ended question of FindInterfaceWith

    c# allow multiple interfaces to be implemented in one class, in this case first one of interfaces will be returned by FindInterfaceWith, because there is no way how to know which of interfaces IA or IB are preferable in general in following sample

    multiple_interfaces_implementing

    Interfaces and classes hierarchy

        public interface IBase {}
        public interface ISomething {}
        public interface IDerivied: IBase {}
        public interface IDeriviedRight: IDerivied {}
        public interface IDeriviedLeft: IDerivied, IDisposable {}
    
        public class AnotherDisposable: IDisposable {
            public void Dispose() {
            }
        }
    
        public class DeriviedLeft: IDeriviedLeft {
            public void Dispose() {
            }
        }
    
        public class SubDeriviedLeft: DeriviedLeft {}
        public class SecondSubDeriviedLeft: DeriviedLeft {}
        public class ThirdSubDeriviedLeft: DeriviedLeft, ISomething {}
    
        public class Another {}
        public class DeriviedRight: IDeriviedRight {}
    

    Test cases

    And set of test cases using NUnit assertions:

    FindBaseClassWith assertions example

    // FindBaseClassWith returns null if one of parameters was an interface. 
    // FindBaseClassWith  return null if any of parameter was null. 
    Assert.That(typeof(DeriviedLeft).FindBaseClassWith(typeof(DeriviedLeft)), Is.EqualTo(typeof(DeriviedLeft)));
    

    FindInterfaceWith assertions example

    // FindInterfaceWith returns null if they don't have common implemented interface. 
    // FindBaseClassWith  return null if any of parameter was null. 
    Assert.That(typeof(DeriviedLeft).FindInterfaceWith(typeof(DeriviedLeft)), Is.EqualTo(typeof(IDeriviedLeft)));
    

    FinAssignableWith assertions example

    Assert.That(typeof(DeriviedLeft).FindAssignableWith(typeof(DeriviedLeft)), Is.SameAs(typeof(DeriviedLeft)));
    

    Discussion at CodeReview

    Review of this answer at codereview.stackexchange.com

    ps:
    Full sources available [here]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is what I want to do. Use this HTML line and have the
Here's the code i keep seeing-- public Task PossiblyAsyncOperation(bool condition) { //this condition means
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>
Here is the code in a function I'm trying to revise. This example works
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here is the script I'm using, copied directly from Google: <script type=text/javascript> var _gaq
Here is the two scripts I have Script 1: <? include('config.php'); $json = $_POST['payload'];
here is the site : http://www.notrepanorama.com at the bottom left, i use a jquery
Here's my problem I have this javascript if (exchRate != ) { function roundthecon()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.