Arvind
Arvind

Reputation: 53

How to create a dictionary in c# which can store Type as value

I am trying to store a Enum class Type as value in Dictionary but i am getting 'Test' is a type, which is not valid in the given context. basically Test is Enum Class.

public enum Test
    {
        Test1 = 1,
        Test2 = 2,
    }

and Dictionary:-
private Dictionary<int, Type> _typeMapping;

Upvotes: 0

Views: 93

Answers (2)

Marco Salerno
Marco Salerno

Reputation: 5203

Solution:

class Program
{
    private static Dictionary<int, Type> TypeMapping { get; set; }
    static void Main(string[] args)
    {
        TypeMapping = new Dictionary<int, Type>();
        Test test1 = Test.Test1;
        TypeMapping.Add((int)test1, test1.GetType());
    }
}

public enum Test
{
    Test1 = 1,
    Test2 = 2
}

Upvotes: 0

Tim Rutter
Tim Rutter

Reputation: 4679

I would guess you are doing something like this:

_typeMapping.Add(1, Test);

When you should do this:

_typeMapping.Add(1, typeof(Test));

Upvotes: 1

Related Questions