© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
2 replies
Bluestopher

Generic Class Help

Hello,

I am working on a PluginManager that contains a dictionary of plugins, which are accessed via keys based off the plugin name. Plugins are generic and have a constraint that the type implements IRecord. I am having an issue with my GetPlugin method because it expects to return IPlugin<IRecord> and not the generic IPlugin<IRecord>. I’m not to sure how to define this since my dictionary of IPlugins is meant to be generic (IPlugin<T>). Does anyone have any ideas on how to fix this or implement it? Also, all my plugins are registered at startup and resolved via DI, which is why I use IRecord as my type of IPlugin since all plugins work on data that implements IRecord. I am a bit stuck currently and tried a bunch of different approaches.

public class PluginManager : IPluginManager
{
    private readonly Dictionary<string, IPlugin<IRecord>> _plugins = new();

    public PluginManager(IEnumerable<IPlugin<IRecord>> plugins)
    {
        Requires.NotNull(plugins, nameof(plugins));

        plugins.ForEach(RegisterPlugin);
    }

    private void RegisterPlugin(IPlugin<IRecord> plugin)
    {
        Requires.NotNull(plugin, nameof(plugin));

        if (_plugins.ContainsKey(plugin.Name))
        {
            throw new ArgumentException($"Unable to register plugin. A plugin with Key: \"{plugin.Name}\" is already registered.");
        }

        _plugins.Add(plugin.Name, plugin);
    }

    public IPlugin<T> GetPlugin<T>(string key) 
        where T : class, IRecord
    {
        if (!_plugins.ContainsKey(key))
        {
            throw new ArgumentException($"Plugin with key \"{key}\" is not registered.");
        }

        return _plugins[key]; // This gives me an error since it cannot return IPlugin<T> since it expects to return IPlugin<IRecord>.
    }
}
public class PluginManager : IPluginManager
{
    private readonly Dictionary<string, IPlugin<IRecord>> _plugins = new();

    public PluginManager(IEnumerable<IPlugin<IRecord>> plugins)
    {
        Requires.NotNull(plugins, nameof(plugins));

        plugins.ForEach(RegisterPlugin);
    }

    private void RegisterPlugin(IPlugin<IRecord> plugin)
    {
        Requires.NotNull(plugin, nameof(plugin));

        if (_plugins.ContainsKey(plugin.Name))
        {
            throw new ArgumentException($"Unable to register plugin. A plugin with Key: \"{plugin.Name}\" is already registered.");
        }

        _plugins.Add(plugin.Name, plugin);
    }

    public IPlugin<T> GetPlugin<T>(string key) 
        where T : class, IRecord
    {
        if (!_plugins.ContainsKey(key))
        {
            throw new ArgumentException($"Plugin with key \"{key}\" is not registered.");
        }

        return _plugins[key]; // This gives me an error since it cannot return IPlugin<T> since it expects to return IPlugin<IRecord>.
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ generic class in generic method
C#CC# / help
3y ago
Struggling with generic class... design?
C#CC# / help
2y ago
Generic XML Node to Class
C#CC# / help
3y ago
✅ Refering to Generic Base Class
C#CC# / help
3y ago