I have the code below for parsing different types. However I realized when deploying to Azure the double etc was different on my local PC. My best bet is due to CultureInfo. I would like to add CultureInfo.InvariantCulture to the code below. However I have not found a way to do this via the delegate approach. Any pointers how to fix this? Or is it not possible for all the types and I need to handle each type differently?
using System;
using System.Collections.Generic;
using System.Text;
namespace Common.Parse
{
public delegate bool ParseDelegate<T>(string s, out T result);
public static class TryParseExtensions
{
public static int? TryParseOrDefaultInt32(this string value)
{
return TryParseOrDefault<int>(value, int.TryParse);
}
public static Int16? TryParseOrDefaultInt16(this string value)
{
return TryParseOrDefault<Int16>(value, Int16.TryParse);
}
public static Int64? TryParseOrDefaultInt64(this string value)
{
return TryParseOrDefault<Int64>(value, Int64.TryParse);
}
public static byte? TryParseOrDefaultByte(this string value)
{
return TryParseOrDefault<byte>(value, byte.TryParse);
}
public static bool? TryParseOrDefaultBoolean(this string value)
{
return TryParseOrDefault<bool>(value, bool.TryParse);
}
public static Single? TryParseOrDefaultSingle(this string value)
{
return TryParseOrDefault<Single>(value, Single.TryParse);
}
public static Double? TryParseOrDefaultDouble(this string value)
{
return TryParseOrDefault<Double>(value, Double.TryParse);
}
public static float? TryParseOrDefaultFloat(this string value)
{
return TryParseOrDefault<float>(value, float.TryParse);
}
public static Decimal? TryParseOrDefaultDecimal(this string value)
{
return TryParseOrDefault<Decimal>(value, Decimal.TryParse);
}
public static DateTime? TryParseOrDefaultDateTime(this string value)
{
return TryParseOrDefault<DateTime>(value, DateTime.TryParse);
}
public static int TryParseInt32(this string value)
{
return TryParse<int>(value, int.TryParse);
}
public static Int16 TryParseInt16(this string value)
{
return TryParse<Int16>(value, Int16.TryParse);
}
public static Int64 TryParseInt64(this string value)
{
return TryParse<Int64>(value, Int64.TryParse);
}
public static byte TryParseByte(this string value)
{
return TryParse<byte>(value, byte.TryParse);
}
public static bool TryParseBoolean(this string value)
{
return TryParse<bool>(value, bool.TryParse);
}
public static Single TryParseSingle(this string value)
{
return TryParse<Single>(value, Single.TryParse);
}
public static Double TryParseDouble(this string value)
{
return TryParse<Double>(value, Double.TryParse);
}
public static Decimal TryParseDecimal(this string value)
{
return TryParse<Decimal>(value, Decimal.TryParse);
}
public static DateTime TryParseDateTime(this string value)
{
return TryParse<DateTime>(value, DateTime.TryParse);
}
public static T TryParse<T>(this string value, ParseDelegate<T> parse) where T : struct
{
T result;
parse(value, out result);
return result;
}
public static T? TryParseOrDefault<T>(this string value, ParseDelegate<T> parse) where T : struct
{
T result;
var succeed = parse(value, out result);
if (succeed)
{
return result;
}
return null;
}
}
}