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

  • Home
  • SEARCH
  • 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 877433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:36:30+00:00 2026-05-15T11:36:30+00:00

Are there any good algorithms for determining the best type to instantiate in order

  • 0

Are there any good algorithms for determining the “best” type to instantiate in order to fulfill a request?

For instance say I have the following classes:

public interface ISometype<T> {}
public class SomeTypeImpl<T>:ISometype<T> {}
public class SomeSpecificTypeImpl<T>:ISometype<T> where T: ISpecificSpecifier {}
public interface ISpecificSpecifier { } 

Suppose a caller wants the best implementation type for this interface. I could implement this particular method like this:

public Type GetBestImplementationType(Type genericParam) {
    try {
        return typeof(SomeSpecificTypeImpl<>).MakeGenericType(genericParam);
    } catch(ArgumentException) {}
    return typeof(SomeTypeImpl<>).MakeGenericType(genericParam);
}

While this implementation will work for this particular case I am more concerned about the generalizations where there may be more than one potential specific implementation and multiple generic parameters:

public Type GetBestImplementationType(Type[] genericParams, Type[] potentialTypes) {
    foreach(var t in potentialTypes) {
        try {
            return t.MakeGenericType(genericParams);
        } catch(ArgumentException) {}
    }
    throw new Exception("unable to find specific implementation type");
}

This should work given the potentialTypes array is provided from most to least specific order. So for answers, either an algorithm implementing this method (or something sufficiently similar) or an algorithm implementing the sort that I could use in this method would do.
[warning: code untested, syntax/logic errors may exist]

  • 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-05-15T11:36:31+00:00Added an answer on May 15, 2026 at 11:36 am

    It seems that:

    1. There is no better way to determine if a generic type’s constraints are met other than to attempt to create the type and catch the exception (it can be done, but seems to take at least as long as the exception method and is far more complex).
    2. Because of #1, it is very difficult [computationally] to order a set of types from most to least specific. Instead in code my solution was to explicitly tell my container how to order the types.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there any good references for synchronisation algorithms? I'm interested in algorithms that synchronize
Are there any named/famous/particularly good algorithms for organizing email messages into threads? I'm looking
Is there any good reference to Algorithms that people use for rare event detection
Are there any good compression algorithms for a large sequence of integers (A/D converter
Is there any good way to deal with the class renaming refactor from Resharper
Are there any good recommendations anyone can provide for a good Javascript editor on
Are there any good, cross platform (SBCL and CLISP at the very least) easy
Is there any good alternative to ASpell? It's nice open source, but haven't been
Are there any good books or website that go over creating a JTable ?
Is there any good tool for working on Dabtabase apart from Toad which requires

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.