Created
July 30, 2014 10:19
-
-
Save jrgcubano/c1a01fd5ab0a1edb72c9 to your computer and use it in GitHub Desktop.
Converter Helper from string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Globalization; | |
using System.Linq; | |
using System.Text; | |
namespace PMS_UI.CommonControls.Converters | |
{ | |
public static class ConverterHelper | |
{ | |
public static object GetValueFromString(object value, Type type) | |
{ | |
//if (null == value) | |
//{ return null; } | |
//else | |
//{ | |
HashSet<Type> numericTypes = new HashSet<Type> | |
{typeof(Byte), | |
typeof(Decimal), | |
typeof(Double), | |
typeof(Int16), | |
typeof(Int32), | |
typeof(Int64), | |
typeof(SByte), | |
typeof(Single), | |
typeof(UInt16), | |
typeof(UInt32), | |
typeof(UInt64) | |
}; | |
if (numericTypes.Contains(type.UnderlyingSystemType)) | |
{ | |
HashSet<Type> intTypes = new HashSet<Type> | |
{typeof(Byte), | |
typeof(Int16), | |
typeof(Int32), | |
typeof(Int64), | |
typeof(SByte), | |
typeof(UInt16), | |
typeof(UInt32), | |
typeof(UInt64) | |
}; | |
string text = ""; | |
if (value == null) | |
text = "0"; | |
else | |
text = value.ToString().Replace(NumberFormatInfo.CurrentInfo.NumberGroupSeparator, String.Empty); | |
if (intTypes.Contains(type.UnderlyingSystemType)) | |
{ | |
try { return Int32.Parse(text); } | |
catch { return null; } | |
} | |
else | |
{ | |
// try { return Double.Parse(text); } | |
try { return Decimal.Parse(text); } | |
catch { return null; } | |
} | |
} | |
else if(type.UnderlyingSystemType == typeof(String) && value == null) | |
{ | |
return ""; | |
} | |
else | |
return value; | |
} | |
//} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment