How to reference a dynamic type using dictionaries?

So here is my code:

public static class ApplicationCacheDictionary
{
    private static Dictionary<string, Type> CacheDictionary = new Dictionary<string, Type>
    {
        {"X1", typeof (class1)},
        {"X2", typeof (class2)},
    };

    public static Type GetMatchingType(string y, string x)
    {
        var key = x + y;
        if (!CacheDictionary.ContainsKey(key))
            return null;
        else
            return CacheDictionary[key];
        return null;
    }
}

And then in another class I have the following code:

dynamic appCache = ApplicationCacheDictionary.GetMatchingType("X", "1");
dynamic result = ApplicationCache.Get<List<appCache>>("X1");

Here is the definition of Get:

public static T Get<T>(string key) where T : class
{
    try
    {
        return (T) Cache.Value[key];
    }
    catch (Exception exception)
    {
        return null;
    }
}

The problem I ran into is the line

dynamic result = ApplicationCache.Get<List<appCache>>("X1");

The following error message appears: "is a variable, but used as a type." At first I tried to solve this problem with the following code:

dynamic result = ApplicationCache.Get<List<dynamic>>("X1");

This compiled, but I got a null value, where should there be one (hardcoding in the actual class instead of dynamic jobs, but I'm trying to get away from it.) Any suggestions on what else can I try to get this job?

+4
source share
1 answer
dynamic result = ApplicationCache.Get<List<appCache>>("X1");

ApplicationCache Get List<appCache> T. , . , .

:

dynamic result = ApplicationCache.Get<dynamic>("X1");

, .

, . , , Type, null. , . ? ?

+6

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


All Articles