Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use strongly-typed like dictionary <string,Type> to associate a string to a class

Tags:

c#

I want to create a dictionary to associate a string to a class for example:

"dog" -> Dog class
"cat" -> Cat class

so I tried this

public Dictionary<String, Type> dic = new Dictionary<String, Type>();
dic.Add("dog", Dog);

but Add doesn't accept Dog.

So what's the syntax ?

like image 982
user310291 Avatar asked Sep 20 '10 11:09

user310291


1 Answers

You should use the typeof operator to get the corresponding Type object:

public Dictionary<String, Type> dic = new Dictionary<String, Type>();
dic.Add("dog", typeof(Dog));
like image 70
Fredrik Mörk Avatar answered Oct 27 '22 00:10

Fredrik Mörk