Can I pass a non-generic type where a generic type is expected?

Posted by Water Cooler v2 on Stack Overflow See other posts from Stack Overflow or by Water Cooler v2
Published on 2010-03-28T19:33:47Z Indexed on 2010/03/28 19:43 UTC
Read the original article Hit count: 190

Filed under:
|

I want to define a set of classes that collect and persist data. I want to call them either on-demand basis, or in a chain-of-responsibility fashion, as the caller pleases.

To support the chaining, I have declared my interface like so:

interface IDataManager<T, K>
{
    T GetData(K args);
    void WriteData(Stream stream);
    void WriteData(T data, Stream stream);

    IDataCollectionPolicy Policy;

    IDataManager<T, K> NextDataManager;
}

But the T's and K's for each concrete types will be different. If I give it like this:

IDataManager<T, K> NextDataManager;

I assume that the calling code will only be able to chain types that have the same T's and K's. Is there a way I can have it chain any type of IDataManager?

One thing that occurs to me is to have IDataManager inherit from a non-generic IDataManager like so:

interface IDataManager { }

interface IDataManager<T, K>: IDataManager
{
    T GetData(K args);
    void WriteData(Stream stream);
    void WriteData(T data, Stream stream);

    IDataCollectionPolicy Policy;

    IDataManager NextDataManager;
}

Is this going to work?

© Stack Overflow or respective owner

Related posts about generics

Related posts about c#