Shared dictionary with value as a shared link interface

I want to have a dictionary in which the values ​​are common objects and will not be the same for each word dictionary. How can this be done, I feel that I am missing something simple.

E.G.


    public interface IMyMainInterface
    {
        Dictionary<string, IMyInterface<T>> Parameters { get; }
    }

    public interface IMyInterface<T>
    {
        T Value
        {
            get;
            set;
        }

        void SomeFunction();
    }

Result:
dic.Add("key1", new MyVal<string>());
dic.Add("key2", new MyVal<int>());

+3
source share
1 answer

You cannot do this because it Tdoes not matter in IMyMainInterface. If your goal is for each value to be an implementation of some IMyInterface<T>, but each value could be an implementation for another T, then you should probably declare a base interface:

public interface IMyInterface
{
    void SomeFunction();
}

public interface IMyInterface<T> : IMyInterface
{
    T Value { get; set; }
}

then

public interface IMyMainInterface
{
    Dictionary<string, IMyInterface> Parameters { get; }
}

EDIT: , , , . , , , , . :

var pair = dictionary.First();
var value = pair.Value;

, value ?


, , T, . , , Ts:

public interface IMyMainInterface<TFoo>
{
    Dictionary<string, IMyInterface<TFoo>> Parameters { get; }
}
+8

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


All Articles