I have code similar to this:
class Foo { Dictionary<Type, Object> _dict; void Create(string myType, string myValue) { var instance = Type.Instanciate(myType) // How do I do this? if (var.IsPrimitive) { var.GetType().Parse(myValue) // I know this is there...how to invoke? Dictionary[instance.GetType()] = instance; } } T GetValue<T>(T myType) { return (T)_dict[T]; } } // Populate with values foo.Create('System.Int32', '15'); foo.Create('System.String', 'My String'); foo.Create('System.Boolean', 'False'); // Access a value bool b = GetValue(b);
So my questions are:
a) How do I instantiate the type
b) Parse the type value from a string when Parse is supported.
Type.GetType()Activator.CreateInstance(You don’t actually need this in your code.)Convert.ChangeTypeNote that if the type isn’t in
mscorlibor the currently executing assembly, you’ll need to include the assembly name (and version information if it’s strongly named).Here’s a complete example using your original code. Note that
GetValuedoesn’t need a normal parameter, as you’ve already given the type parameter (T).