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 8571457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:47:09+00:00 2026-06-11T18:47:09+00:00

I have a dictionary of registered types. Dictionary<Type, Type> knownTypes = new Dictionary<Type, Type>()

  • 0

I have a dictionary of registered types.

Dictionary<Type, Type> knownTypes = new Dictionary<Type, Type>() {
   { typeof(IAccountsPlugin), typeof(DbTypeA) },
   { typeof(IShortcodePlugin), typeof(DbTypeB) }
};

I need to test if an object implements a specific interface as a key, and if so instantiate the counterpart value.

public Plugin FindDbPlugin(object pluginOnDisk)
{
    Type found;
    Type current = type.GetType();

    // the below doesn't work - need a routine that matches against a graph of implemented interfaces
    knownTypes.TryGetValue(current, out found); /
    if (found != null)
    {
        return (Plugin)Activator.CreateInstance(found);
    }
}

All types that will be created (in this case DbTypeA, DbTypeB and so forth) will only ever derive from type Plugin.

The object that is passed in may be inherited from one of the types we are trying to match (i.e. IAccountsPlugin) by several generations of inheritance. This is why I cannot do pluginOnDisk.GetType().

Is there a way to test if an object implements a type, and then create the new instance of that type, with a dictionary lookup rather than brute forcing and testing typeof in a long loop?

  • 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-11T18:47:10+00:00Added an answer on June 11, 2026 at 6:47 pm

    Change this method to be generic, and specify the type of the object you are looking for:

    public Plugin FindDbPlugin<TKey>(TKey pluginOnDisk)
    {
        Type found;
        if (knownTypes.TryGetValue(typeof(TKey), out found) && found != null)
        {
            Plugin value = Activator.CreateInstance(found) as Plugin;
            if (value == null)
            {
                throw new InvalidOperationException("Type is not a Plugin.");
            }
    
            return value;
        }
    
        return null;
    }
    

    Example:

    IAccountsPlugin plugin = ...
    Plugin locatedPlugin = FindDbPlugin(plugin);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have dictionary that is defined as: Dictionary<string, object> d = new Dictionary<string, object>();
I have dictionary, like Dictionary<string, bool> accValues = new Dictionary<string, bool>() And I want
I have a dictionary public static IDictionary<string, IList<string>> checksCollection = new Dictionary<string, IList<string>>(); I
The problem: I have Dictionary<String, String> that I need an alias to, but I
I have MVC 3 application and I have dictionary of type Dictionary<String,int> in c#.
I have Dictionary<long, Location> PostalCodes . While i m adding new elements to this
I have Dictionary<Guid, DateTime> d = new Dictionary<Guid, DateTime>(); How I can get an
Have Dictionary <Int64, byte> that gets used a lot. I mean in a loop
Does VBA have dictionary structure? Like key<>value array?
I have a Dictionary where the key and value are both strings. It's possible

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.