/// <summary> /// Converts a type using the implicit or explicit operators. If both fails it will try to /// convert the value with <see cref="System.Convert.ChangeType(object, Type)"/>. /// </summary> /// <param name="source">The object to convert</param> /// <param name="sourceType">The type of the object to convert</param> /// <param name="targetType">The type to convert to</param> /// <returns>A new instance of <paramref name="targetType"/>.</returns> /// <example> /// <code> /// public class Bla /// { /// public string Value { get; set; } /// /// public static implicit operator Bla(string value) => new Bla { Value = value }; /// /// public static implicit operator Bla(int value) => new Bla { Value = value.ToString() }; /// } /// </code> /// The code can be called like following: /// <code> /// var bar = "Test Test".As(typeof(string), typeof(Bla)); /// </code> /// </example> public static object As(this object source, Type sourceType, Type targetType) { var method = ImplicitExplicitConvertionCache.Get(sourceType, targetType); if (method != null) { return(method(source)); } #if NETFX_CORE if (targetType.GetTypeInfo().IsEnum&& source != null) #else if (targetType.IsEnum && source != null) #endif { return(Enum.Parse(targetType, source.ToString())); } return(System.Convert.ChangeType(source, targetType)); }
/// <summary> /// Performs a cast between compatible reference types. If a convertion is not possible then /// null is returned. As a last resort it will use <see /// cref="System.Convert.ChangeType(object, Type)"/>. /// <para/> /// Tries to use the implicit and explicit operators if exists when convertion with 'as' /// returns null. /// </summary> /// <typeparam name="T">The <see cref="Type"/> to convert to</typeparam> /// <param name="source">The object to convert</param> /// <returns>The object casted to <typeparamref name="T"/></returns> /// <example> /// In the following code example, the 'As' extension is used to convert a returned object /// via the implicit operator. /// <code> /// public interface IFoo /// { /// string Name {get;} /// string Description {get;} /// } /// /// public class Foo : IFoo /// { /// public string Name {get; set;} /// public string Description {get; set;} /// } /// /// public class Bar : BarBase /// { /// private IFoo internalFoo; /// /// private Bar(IFoo foo) /// { /// this.internalFoo = foo; /// } /// /// public void DoSomeStuff() /// { /// } /// /// public void DoSomeOtherStuff() /// { /// } /// /// public static implicit operator Bar(Foo value) => new Boo(value); /// public static implicit operator Foo(Bar value) => value.internalFoo; /// } /// /// public class SomeOtherClass /// { /// public IFoo GetFooFromSomewhere(string fooId) => new Foo { Name = "A Foo", Description = "This is the foo you are looking for." }; /// } /// </code> /// The code can be called like following: /// <code> /// var bar = someOtherClassInstance.GetFooFromSomewhere("fooThatINeed").As<Bar>(); /// </code> /// </example> public static T As <T>(this object source) where T : class { var result = source as T; if (result == null && source != null) { var method = ImplicitExplicitConvertionCache.Get(source.GetType(), typeof(T)); if (method != null) { return(method(source) as T); } #if NETFX_CORE if (typeof(T).GetTypeInfo().IsEnum&& source != null) #else if (typeof(T).IsEnum && source != null) #endif { return(Enum.Parse(typeof(T), source.ToString()) as T); } return(System.Convert.ChangeType(source, typeof(T)) as T); } return(result); }
/// <summary> /// Converts a string to the type defined by <paramref name="targetType"/> /// </summary> /// <param name="value">The string value to convert</param> /// <param name="targetType">The type to convert the string to</param> /// <param name="numberformat"> /// An object that supplies culture-specific formatting information about <paramref name="value"/>. /// </param> /// <returns>The converted value</returns> public static object Convert(this string value, Type targetType, NumberFormatInfo numberformat) { /* * TypeConverter does not exist in UWP that is why we built our own */ #if WINDOWS_UWP || NETCORE if ((targetType.IsNullable() || !targetType.GetTypeInfo().IsValueType) && string.IsNullOrEmpty(value)) #else if ((targetType.IsNullable() || !targetType.IsValueType) && string.IsNullOrEmpty(value)) #endif { return(null); } if (string.IsNullOrEmpty(value)) /* This is for value types ... This will always return false for non value types*/ { return(targetType.GetDefaultInstance()); } if (targetType.IsNullable()) { targetType = Nullable.GetUnderlyingType(targetType); } #if WINDOWS_UWP || NETCORE if (targetType.GetTypeInfo().IsEnum) #else if (targetType.IsEnum) #endif { if (value.All(char.IsDigit)) { return(Enum.ToObject(targetType, value.ToLong())); } else { return(Enum.Parse(targetType, value)); } } if (targetType == typeof(string)) { return(value); } if (targetType == typeof(int)) { return(value.ToInteger(numberformat)); } if (targetType == typeof(uint)) { return(value.ToUInteger(numberformat)); } if (targetType == typeof(long)) { return(value.ToLong(numberformat)); } if (targetType == typeof(ulong)) { return(value.ToULong(numberformat)); } if (targetType == typeof(byte)) { return(value == "" ? (byte)0 : (byte)value[0]); } if (targetType == typeof(sbyte)) { return(value == "" ? (sbyte)0 : (sbyte)value[0]); } if (targetType == typeof(float)) { return(value.ToFloat(numberformat)); } if (targetType == typeof(double)) { return(value.ToDouble(numberformat)); } if (targetType == typeof(decimal)) { return(value.ToDecimal(numberformat)); } if (targetType == typeof(bool)) { return(value.ToBool()); } if (targetType == typeof(char)) { return(value == "" ? (char)0 : value[0]); } if (targetType == typeof(short)) { return(value.ToShort(numberformat)); } if (targetType == typeof(ushort)) { return(value.ToUShort(numberformat)); } if (targetType == typeof(IntPtr)) { return((IntPtr)value.ToInteger(numberformat)); } if (targetType == typeof(UIntPtr)) { return((UIntPtr)value.ToUInteger(numberformat)); } if (targetType == typeof(DateTime)) { return(DateTime.Parse(value)); } if (targetType == typeof(DateTimeOffset)) { return(DateTimeOffset.Parse(value)); } if (targetType == typeof(TimeSpan)) { return(TimeSpan.Parse(value)); } if (targetType == typeof(Guid)) { return(Guid.Parse(value)); } var op = ImplicitExplicitConvertionCache.Get(typeof(string), targetType); if (op != null) { return(op(value)); } return(targetType.GetDefaultInstance()); }