using System.Globalization;
public struct NullableParsable<T> where T : struct
public static T? Parse1(string s)
string tName = typeof(T).Name;
case "Int32": result = string.IsNullOrEmpty(s) ? null : (int?)int.Parse(s); break;
case "Decimal": result = string.IsNullOrEmpty(s) ? null : (decimal?)decimal.Parse(s); break;
default: throw new NotImplementedException("Unmanaged type: "+ tName);
public static T? Parse2(string s)
string tName = typeof(T).Name;
result = int.TryParse(s, out intValue) ? (int?)intValue : null;
result = decimal.TryParse(s, out decValue) ? (decimal?)decValue : null;
default: throw new NotImplementedException("Unmanaged type: "+ tName);
public static T? Parse3(string s)
string tName = type.Name;
MethodInfo methodInfo = type.GetMethod("Parse", new[] { typeof(string) });
result = methodInfo.Invoke(null, new[] { s });
public static T? Parse3(string s, IFormatProvider provider)
string tName = type.Name;
MethodInfo methodInfo = type.GetMethod("Parse", new[] { typeof(string), typeof(IFormatProvider) });
result = methodInfo.Invoke(null, new object[] { s, provider });
public static T? Parse3(string s, NumberStyles style)
string tName = type.Name;
MethodInfo methodInfo = type.GetMethod("Parse", new[] { typeof(string), typeof(NumberStyles) });
result = methodInfo.Invoke(null, new object[] { s, style });
public static T? Parse3(string s, NumberStyles style, IFormatProvider provider)
string tName = type.Name;
MethodInfo methodInfo = type.GetMethod("Parse", new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider) });
result = methodInfo.Invoke(null, new object[] { s, style, provider });
public static T? Parse4(string s)
string tName = type.Name;
MethodInfo methodInfo = type.GetMethod("TryParse", new[] { typeof(string), type.MakeByRefType() });
object[] args = new object[] { s, null };
bool parsed = (bool)methodInfo.Invoke(null, args);
dynamic result = parsed ? args[1] : null;
public static T? Parse4(string s, IFormatProvider provider)
string tName = type.Name;
MethodInfo methodInfo = type.GetMethod("TryParse", new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), type.MakeByRefType() });
object[] args = new object[] { s, NumberStyles.Any, provider, null };
bool parsed = (bool)methodInfo.Invoke(null, args);
dynamic result = parsed ? args[3] : null;
public static T? Parse4(string s, NumberStyles style)
string tName = type.Name;
MethodInfo methodInfo = type.GetMethod("TryParse", new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), type.MakeByRefType() });
object[] args = new object[] { s, style, CultureInfo.CurrentCulture, null };
bool parsed = (bool)methodInfo.Invoke(null, args);
dynamic result = parsed ? args[3] : null;
public static T? Parse4(string s, NumberStyles style, IFormatProvider provider)
string tName = type.Name;
MethodInfo methodInfo = type.GetMethod("TryParse", new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), type.MakeByRefType() });
object[] args = new object[] { s, style, provider, null };
bool parsed = (bool)methodInfo.Invoke(null, args);
dynamic result = parsed ? args[3] : null;
public static void Main()
decimal? d1 = decimal.TryParse(s, out d) ? (decimal?)d : null;
decimal? d2 = string.IsNullOrEmpty(s) ? null : (decimal?)decimal.Parse(s);
decimal? d3 = NullableParsable<decimal>.Parse1(s);
decimal? d4 = NullableParsable<decimal>.Parse2(s);
decimal? d5 = NullableParsable<decimal>.Parse3(s);
decimal? d6 = NullableParsable<decimal>.Parse3(s, CultureInfo.CurrentCulture);
decimal? d7 = NullableParsable<decimal>.Parse4(s);
decimal? d8 = NullableParsable<decimal>.Parse4(s, CultureInfo.CurrentCulture);
Console.WriteLine("Hello d1 = "+ d1);
Console.WriteLine("Hello d2 = "+ d2);
Console.WriteLine("Hello d3 = "+ d3);
Console.WriteLine("Hello d4 = "+ d4);
Console.WriteLine("Hello d5 = "+ d5);
Console.WriteLine("Hello d6 = "+ d6);
Console.WriteLine("Hello d7 = "+ d7);
Console.WriteLine("Hello d8 = "+ d8);
double? f = NullableParsable<double>.Parse1(s);