public static T To <T>(this IConvertible value) { try { Type t = typeof(T); Type u = Nullable.GetUnderlyingType(t); if (u != null) { if (value == null || value.Equals("")) { return(default(T)); } return((T)Convert.ChangeType(value, u)); } if (value == null || value.Equals("")) { return(default(T)); } return((T)Convert.ChangeType(value, t)); } catch { return(default(T)); } }
public static T To <T>(this IConvertible value, IConvertible ifError) { try { Type t = typeof(T); Type u = Nullable.GetUnderlyingType(t); if (u != null) { if (value == null || value.Equals("")) { return((T)ifError); } return((T)Convert.ChangeType(value, u)); } else { if (value == null || value.Equals("")) { return((T)(ifError.To <T>())); } return((T)Convert.ChangeType(value, t)); } } catch { return((T)ifError); } }
public static T ConvertTo <T>(this IConvertible convertibleValue) { if (null == convertibleValue || convertibleValue.Equals(string.Empty)) { return(default(T)); } if (!typeof(T).IsGenericType) { return((T)Convert.ChangeType(convertibleValue, typeof(T))); } else { Type genericTypeDefinition = typeof(T).GetGenericTypeDefinition(); if (genericTypeDefinition == typeof(Nullable <>)) { return((T)Convert.ChangeType(convertibleValue, Nullable.GetUnderlyingType(typeof(T)))); } } throw new InvalidCastException(string.Format("Invalid cast from type \"{0}\" to type \"{1}\".", convertibleValue.GetType().FullName, typeof(T).FullName)); }
/// <summary> /// Converts one type to another /// </summary> /// <example> /// var age = "28"; /// var intAge = age.To<int>(-1); /// var doubleAge = intAge.To<double>(-1); /// var decimalAge = doubleAge.To<decimal>(-1); /// </example> /// <typeparam name="T">Type to convert to</typeparam> /// <param name="value">Item to convert</param> /// <param name="ifError">Value to return if there is an error</param> /// <returns></returns> public static T ConvertTo <T>(this IConvertible value, T ifError) { try { if (value == null || value.Equals("")) { return(ifError); } Type t = typeof(T); Type u = Nullable.GetUnderlyingType(t); return(u != null ? (T)Convert.ChangeType(value, u) : (T)Convert.ChangeType(value, t)); } catch { return(ifError); } }
public static T To <T>(this IConvertible value) where T : new() { _ = value ?? throw new ArgumentNullException(nameof(value)); try { if (value.Equals(string.Empty)) { return(new T()); } var t = typeof(T); var u = Nullable.GetUnderlyingType(t); return(u != null ? (T)Convert.ChangeType(value, u, CultureInfo.CurrentCulture) : (T)Convert.ChangeType(value, t, CultureInfo.CurrentCulture)); } catch { return(new T()); } }
public static bool Equals <T1, T2>(T1 first, T2 second) { try { IConvertible firstConveted = CastToConvertible(first); IConvertible secondConverted = CastToConvertible(second); // standard Equals cannot compare two different types, // so here the second value is // converted to the type of the first value var convertedToFirst = (IConvertible)Convert.ChangeType( secondConverted, firstConveted.GetTypeCode()); return(firstConveted.Equals(convertedToFirst)); } catch (Exception) { // an exception might be caught in two cases: // 1. One of the values cannot be converted // to IConvertible interface. // 2. The second value cannot be converted // to the type of the first value. return(false); } }
override protected void EvaluateOutputs() { if (ShouldOutput()) { outputPorts[0].Value = queue.Dequeue().value; EmitEvaluationRequired(); } else { outputPorts[0].Value = null; } IConvertible value = InValue(0); if (object.ReferenceEquals(value, lastInput) || (!object.ReferenceEquals(value, null) && value.Equals(lastInput))) { return; } lastInput = value; if (!ValueToBool(value)) { return; } int delay = Math.Max(1, InInt(1)); int outputTick = Manager.CurrentTick + delay; QueueEntry entry = new QueueEntry(outputTick, value); if (queue.Count >= queue.MaxSize) { queue.Dequeue(); } queue.Enqueue(entry, outputTick); }
private object ExecuteBooleanOperator(IConvertible left, IConvertible right, CodeBinaryOperatorType op) { bool flag = false; switch (op) { case CodeBinaryOperatorType.IdentityInequality: return (left != right); case CodeBinaryOperatorType.IdentityEquality: return (left == right); case CodeBinaryOperatorType.ValueEquality: return left.Equals(right); case CodeBinaryOperatorType.BitwiseOr: case CodeBinaryOperatorType.BitwiseAnd: case CodeBinaryOperatorType.LessThan: case CodeBinaryOperatorType.LessThanOrEqual: case CodeBinaryOperatorType.GreaterThan: case CodeBinaryOperatorType.GreaterThanOrEqual: return flag; case CodeBinaryOperatorType.BooleanOr: return (left.ToBoolean(null) || right.ToBoolean(null)); case CodeBinaryOperatorType.BooleanAnd: return (left.ToBoolean(null) && right.ToBoolean(null)); } return flag; }