Created
December 14, 2012 11:55
-
-
Save Cheesebaron/4284902 to your computer and use it in GitHub Desktop.
This file contains 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; | |
using System.Collections.Generic; | |
using System.Globalization; | |
namespace Giraffatitan | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var types = new List<TestObject> | |
{ | |
new TestObject(typeof(sbyte)) { TestValues = new [] { sbyte.MinValue, sbyte.MaxValue} }, | |
new TestObject(typeof(byte)) { TestValues = new [] { byte.MinValue, byte.MaxValue} }, | |
new TestObject(typeof(short)) { TestValues = new [] { short.MinValue, short.MaxValue} }, | |
new TestObject(typeof(ushort)) { TestValues = new [] { ushort.MinValue, ushort.MaxValue} }, | |
new TestObject(typeof(int)) { TestValues = new [] { int.MinValue, int.MaxValue} }, | |
new TestObject(typeof(uint)) { TestValues = new [] { uint.MinValue, uint.MaxValue} }, | |
new TestObject(typeof(long)) { TestValues = new [] { long.MinValue, long.MaxValue} }, | |
new TestObject(typeof(ulong)) { TestValues = new [] { ulong.MinValue, ulong.MaxValue} }, | |
new TestObject(typeof(decimal)) { TestValues = new [] { decimal.MinValue, decimal.MaxValue} }, | |
new TestObject(typeof(double)) { TestValues = new [] { double.MinValue, double.MaxValue} }, | |
new TestObject(typeof(float)) { TestValues = new [] { float.MinValue, float.MaxValue} }, | |
}; | |
foreach (var t in types) | |
{ | |
foreach (var u in types) | |
{ | |
Console.WriteLine("Converting {0} to {1}", t.Type, u.Type); | |
foreach (var v in t.TestValues) | |
{ | |
try | |
{ | |
var converted = MakeSafeValue(u.Type, v); | |
Console.WriteLine("{0} ({1}) converted to {2} ({3})", v, t.Type, converted, converted.GetType()); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine("Could not convert {0} ({1}) to {2}", v, t.Type, u.Type); | |
} | |
} | |
Console.WriteLine("\n"); | |
} | |
} | |
} | |
public static object MakeSafeValue(Type propertyType, object value) | |
{ | |
var safeValue = value; | |
if (!propertyType.IsInstanceOfType(value)) | |
{ | |
if (propertyType.IsValueType && propertyType.IsGenericType) | |
{ | |
var underlyingType = Nullable.GetUnderlyingType(propertyType); | |
safeValue = Convert.ChangeType(value, underlyingType, CultureInfo.CurrentUICulture); | |
} | |
else if (propertyType == typeof(string)) | |
{ | |
if (value != null) | |
{ | |
safeValue = value.ToString(); | |
} | |
} | |
else | |
{ | |
safeValue = Convert.ChangeType(value, propertyType, CultureInfo.CurrentUICulture); | |
} | |
} | |
return safeValue; | |
} | |
} | |
public class TestObject | |
{ | |
public TestObject(Type t) | |
{ | |
Type = t; | |
} | |
public Type Type { get; set; } | |
public IEnumerable TestValues { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment