Wednesday 15 September 2010

c# - How to create Dictionary to store the type of a child class -


How do I create a dictionary to store, in terms of value, type that receives another class is?

Example:

  Dictionaries & lt; String, typef (guardian) & gt; Dict = new dictionary & lt; String, typef (guardian) & gt; (); Dict ["key1"] = Typhus (hair 1); Dict ["key2"] = Typef (Child 2); Dict ["key3"] = Typhus (child 3); Public essence class guardian {} public class Child1: Parent {} Public Class Child2: Parent {} Public Class Child3: Parent {}   

I do not store this example Want to, but the type of class

EDIT: Sorry for my poor explanation, what I really want to do. I am looking for a way to store the type and ensure that this type of parent has inherited I want to be type-safe and make sure that the type of store is the parent's child only The way, for now, I thought how to do it. But it is not what I really want. I would like to do this

  dictionary; String, typef (guardian) & gt; Dict = ...   

Any ideas?

I think you only have to dictionary> String, type & gt; , then when you add something, you should do it;

  dict.Add ("key1", typeof (child1)); Edit: As stated in Answer to Away, you can use the  GetType ()  method on an example if you use a type on the runtime If you want to add it in the compile time, you will usually use  typeof  on the square.   

No comments:

Post a Comment