NullableParsable.cs

CSharp · Console

/Program.cs

using System;
using System.Runtime;
using System.Reflection;
using System.Globalization;

// Type T in interface list is not an interface
//public struct NullableParsable<T> : Nullable<T> where T : struct { }

public struct NullableParsable<T> where T : struct
{
	T? nullableValue;

	public static T? Parse1(string s)
	{
		dynamic result = null;
		string tName = typeof(T).Name;
		
		switch(tName)
		{
			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);
		}
		return result;
	}
	
	public static T? Parse2(string s)
	{
		dynamic result = null;
		string tName = typeof(T).Name;
		
		switch (tName)
		{
			case "Int32":
			int intValue;
			result = int.TryParse(s, out intValue) ? (int?)intValue : null;
			break;
			
			case "Decimal":
			decimal decValue;
			result = decimal.TryParse(s, out decValue) ? (decimal?)decValue : null;
			break;
			
			default: throw new NotImplementedException("Unmanaged type: "+ tName);
		}
		
		return result;
	}
	
    public static T? Parse3(string s)
	{
		dynamic result = null;
		Type type = typeof(T);
		string tName = type.Name;
		MethodInfo methodInfo = type.GetMethod("Parse", new[] { typeof(string) });
		result = methodInfo.Invoke(null, new[] { s });
		return result;
	}
	
	public static T? Parse3(string s, IFormatProvider provider)
	{
		dynamic result = null;
		Type type = typeof(T);
		string tName = type.Name;
		MethodInfo methodInfo = type.GetMethod("Parse", new[] { typeof(string), typeof(IFormatProvider) });
		result = methodInfo.Invoke(null, new object[] { s, provider });
		return result;
	}
	
	public static T? Parse3(string s, NumberStyles style)
	{
		dynamic result = null;
		Type type = typeof(T);
		string tName = type.Name;
		MethodInfo methodInfo = type.GetMethod("Parse", new[] { typeof(string), typeof(NumberStyles) });
		result = methodInfo.Invoke(null, new object[] { s, style });
		return result;
	}
	
	public static T? Parse3(string s, NumberStyles style, IFormatProvider provider)
	{
		dynamic result = null;
		Type type = typeof(T);
		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 });
		return result;
	}
	
    public static T? Parse4(string s)
	{
		Type type = typeof(T);
		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;
		return result;
	}
	
	public static T? Parse4(string s, IFormatProvider provider)
	{
		Type type = typeof(T);
		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;
		return result;
	}
	
	public static T? Parse4(string s, NumberStyles style)
	{
		Type type = typeof(T);
		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;
		return result;
	}
	
	public static T? Parse4(string s, NumberStyles style, IFormatProvider provider)
	{
		Type type = typeof(T);
		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;
		return result;
	}
}
					
public class Program
{
	public static void Main()
	{
		string s = "123.45";
		decimal d;
		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);
	}
}
;