3

Currently im facing the problem I need to create an instance of Dictionary with any type of its cases. The types are delivered by the arguments of the method. I do not simply want to create a dynamic or object type Dictionary, because this faces the user alot of convertion problems by using my library. I also cannot use a simple constructor, because my method fills the Dictionary with data (that is stored in a file) in real. It's important to create the specific dictionary by Type variables. This is what im thinking of:

public static Dictionary<dynamic, dynamic> createDict(Type type1, Type type2)
{
    // how to create the dictionary?
    // im filling my dictionary with any data ...
    return dict;
}

Here the user calls my method:

Dictionary<string, int> dict = MyLib.createDict(typeof(string), typeof(int));
// or
MyOwnType myInstance = new MyOwnType();
Dictionary<string, MyOwnType> dict = MyLib.createDict(typeof(string), myInstance.GetType());
Martin Braun
  • 10,906
  • 9
  • 64
  • 105

2 Answers2

6
Type dictType = typeof(Dictionary<, >).MakeGenericType(Type1, Type2);
var dict = Activator.CreateInstance(dictType);
  • Get the Type for Dictionary
  • Create a generic type with the specified Types using MakeGenericType
  • Create an instance of the generic type using Activator.CreateInstance
Thomas
  • 1,281
  • 8
  • 22
  • oh so simple, i tried this with the target type first, but var seems to be working finally. However, i realized it with dynamic to be able to call my Dictionary methods to fill my dictionary. Thank you. – Martin Braun Jul 30 '12 at 08:40
  • 1
    How do you `Add` items to the newly created Dictionary? `dict.Add("keyName", "value")` won't compile because `dict` is recognized as just a general `object`. – Gary O. Stenstrom Jun 16 '21 at 14:53
4

Using Generics you can do the following:

public static Dictionary<T, U> CreateDict<T,U>()
{
    Dictionary<T,U> dict = new Dictionary<T,U>();

    // Fill dictionary with data     

    return dict;
}

The user then can call this function:

var myDict1 = CreateDict<string,int>();
var myDict2 = CreateDict<string,MyOwnType>();
Wouter de Kort
  • 39,090
  • 12
  • 84
  • 103
  • Yes, the problem is just, it can be any type, even instances of own types by the user itself. I am confused how to create the dictionary dynamically in that case. – Martin Braun Jul 30 '12 at 08:06
  • The user will supply the types. In the example you give, the user is passing a string and int as parameters. It doesn't matter if these are custom types or not. I will edit my answer with an example – Wouter de Kort Jul 30 '12 at 08:08
  • i updated my first post with an example, thank you. – Martin Braun Jul 30 '12 at 08:10
  • thanks for your effort, however, in my library i cannot use this by several reasons. However i learned alot from it, thank you very much. – Martin Braun Jul 30 '12 at 08:42
  • My current (just finished) project is a serializer that even works with dictionaries. The problem appeared on my deserializeDict method. The user calls the method with the XML file path and the type of the dictionary, i get the child types by GetGenericArguments(). So i got the types in Type variables before i create the dictionary. Its alot of easier for the user just transmit the type of the Dictionary instead of both child types like on your way. However, thx. – Martin Braun Jul 30 '12 at 10:23