Friday, 24 February 2012

How to Convert a Value to a Type when both defined in String?

static void Main(string[] args) {
Object result =
"System.Nullable`1[[System.DateTime, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089]]",
"2012-02-23 10:00:00");

public static Object ConvertValue(string typeInString, string value) {
Type originalType = Type.GetType(typeInString);

var underlyingType = Nullable.GetUnderlyingType(originalType);

// if underlyingType has null value, it means the original type wasn't nullable
object instance = Convert.ChangeType(value, underlyingType ?? originalType);

return instance; } 

No comments: