I'm trying to make something like this work.
using System;
using System.Collections.Generic;
namespace RaindancerWeb.Api {
public class Class1 {
public static void Main() {
IDictionary<string, Type> dataKeys = new Dictionary<string, Type>() {
{ "GiveMeAString", typeof(string)},
{ "GiveMeAnInt", typeof(int)},
{ "GiveMeADouble", typeof(double)}
};
MySerialise(dataKeys);
}
public static void MySerialise(IDictionary<string, Type> extraDataKeys) {
foreach (KeyValuePair<string, Type> pair in extraDataKeys) {
string s = DoSomething<pair.Value>(pair.Key);
Console.WriteLine(s);
}
}
private static string DoSomething<T>(string key) {
// Use the Key, to retrieve some info from Database, and parse it to type T
// Return some serialisation of the data as a string.
throw new NotImplementedException();
}
}
}
The DoSomething<pair.Value>
throws an error, as I cannot use a variable as a type.
Is there a way to make something like this work?