Dictionary<T, Func>: how to use T as Func's generic type?

Viewed 1096

I didn't know how to express it clearly.

I have this interface:

interface IConverter
{
    Dictionary<Type, Func<string, object>> ConversionMethods { get; }
}

Basically, it defines a contract saying that a class implementing it should provide conversion methods for all the custom types it uses (be them enums or anything).

Is it possible to replace object in Func's generic types with its corresponding dictionary key's type (so it is impossible to have two unmatching types)?

I think it's not possible, but the alternatives are a bit annoying (using dynamic or object, creating a specialized dictionary...).


edit 1: Imaginary exemple of use

interface IConverter
{
    Dictionary<Type, Func<string, object>> GetConversionMethods();
}

enum A
{
    AA,AB,AC
}

enum B
{
    BA, BB, BC
}

class blah : IConverter
{
    public Dictionary<Type, Func<string, object>> GetConversionMethods()
    {
        var d = new Dictionary<Type, Func<string, object>>
        {
            {
                typeof(A),
                (s) =>
                    {
                        // here, I could return whatever I want because the 'Func' returns 'object'
                        return s == "AA" ? A.AA : s == "AB" ? A.AB : A.AC;
                    }
            },
            {
                typeof(B),
                (s) =>
                    {
                        // same
                        return s == "BA" ? B.BA : s == "BB" ? B.BB : B.BC;
                    }
            }
        };
        return d;
    }

    void blahah()
    {
        // and here, I also get an `object`, where I would like to have a A
        GetConversionMethods()[typeof(A)]("123");
    }
}
2 Answers
Related