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

I want to define a set of classes that collect and store data. I want to call them on demand or on the principle of a chain of responsibility, as the caller likes it.

To maintain the chain, I declared my interface as follows:

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 T and K for each particular type will be different. If I give it like this:

IDataManager<T, K> NextDataManager;

I assume that the calling code can only bind types that have the same T and K. Is there a way I can bind it to any type of IDataManager?

One thing that arises for me is to inherit the IDataManager from the non-generic IDataManager as follows:

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;
}

Will this work?

+3
source share
2

, :

interface IDataManager
{
    void WriteData(Stream stream);
    IDataCollectionPolicy Policy { get; set; }
    IDataManager NextDataManager { get; set; }
}

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

, . ( ) /, /, / .

+2

Source: https://habr.com/ru/post/1738878/


All Articles