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.
2 Replies
I don’t think type casting the return type for the GetPlugin is the answer for this. I believe that pushes the problem under the rug of what I am trying to do but am doing it incorrectly,
Theres no way to do this that doesnt involve casting the type. You effectively want a list of
IPlugin<T>
but each one can have a different T
, which means they have different types.
At some point, some code is going to have to perform a cast to the type you need.
The main question I have to ask is, what is the T
is used for, like to start, what is the definition of the interface IRecord<T>
look ing like at the moment?