public void Can_get_Nullable_number() { int?null1 = 1; var number = DynamicNumber.GetNumber(null1.GetType()); Assert.That(number.ToString(null1), Is.EqualTo("1")); }
public void Can_text_for_numbers() { object x = null; Assert.That(DynamicNumber.TryParse("(", out x), Is.False); Assert.That(DynamicNumber.TryParse("-", out x), Is.False); }
private static object Add2 <T, R>(T lhs, R rhs) { var result = DynamicNumber.Add(lhs, rhs); "{0}: {1} + {2} = {3} ({4})".Print(typeof(T).Name, lhs, rhs, result, result.GetType()); return(result); }
static void Test(string[] args) { // Creating the first dynamic number. dynamic number = new DynamicNumber(); // Creating properties and setting their values // for the dynamic number. // The TrySetMember method is called. number.Textual = "One"; number.Numeric = 1; // Printing out properties. The TryGetMember method is called. Console.WriteLine( number.Textual + " " + number.Numeric); dynamic negativeNumber = new DynamicNumber(); // Performing a mathematical negation. // TryUnaryOperation is called. negativeNumber = -number; Console.WriteLine( negativeNumber.Textual + " " + negativeNumber.Numeric); // The following statement produces a run-time exception // because the unary plus operation is not implemented. // negativeNumber = +number; }
public void Does_right_operation() { Assert.That(DynamicNumber.Add(8, 2) is int a && a == 10); Assert.That(DynamicNumber.Sub(8, 2) is int s && s == 6); Assert.That(DynamicNumber.Mul(8, 2) is int m && m == 16); Assert.That(DynamicNumber.Div(8, 2) is int d && d == 4); }
public void TryParseIntoBestFit_tests() { JsConfig.TryParseIntoBestFit = true; Assert.That(DynamicNumber.TryParse("1", out object result) && result is byte b && b == 1); JsConfig.TryParseIntoBestFit = false; }
public void Dynamic_number_examples() { object objInt = 1; object objDouble = 1.1; Assert.That(DynamicNumber.Add(objInt, objDouble) is double d1 && d1 == 2.1); Assert.That(DynamicNumber.Multiply('2', "1.1") is double d2 && d2 == 2.2); Assert.That(DynamicNumber.TryParseIntoBestFit("1", out object result) && result is byte b && b == 1); }
void Update() { if (Input.GetKeyDown(KeyCode.A)) { int hurt = -100; DynamicNumber.ChangeNum(t, hurt); currentHp += hurt; DynamicImage.MoveImageByFillAmount(img, currentHp / (float)totalHp, 1f); } }
public void Does_convert_string_to_appropriate_popular_type() { object o; Assert.That(DynamicNumber.TryParse("1", out o) && o is int i && i == 1); Assert.That(DynamicNumber.TryParse(int.MaxValue.ToString(), out o) && o is int imax && imax == int.MaxValue); Assert.That(DynamicNumber.TryParse((int.MaxValue + (long)1).ToString(), out o) && o is long l && l == int.MaxValue + (long)1); Assert.That(DynamicNumber.TryParse((long.MaxValue + (double)1).ToString(CultureInfo.InvariantCulture), out o) && o is double d ? d : 0, Is.EqualTo(long.MaxValue + (double)1).Within(10000)); Assert.That(DynamicNumber.TryParse("1.1", out o) && o is double d2 && d2 == 1.1); }
public override object Evaluate(object lhs, object rhs) { if (lhs is string || rhs is string) { var lhsString = lhs.ConvertTo <string>(); var rhsString = rhs.ConvertTo <string>(); return(string.Concat(lhsString, rhsString)); } return(DynamicNumber.Add(lhs, rhs)); }
public object coerce(string str) => DynamicNumber.TryParse(str, out var numValue) ? numValue : str.StartsWith(DateTimeSerializer.WcfJsonPrefix) ? DateTimeSerializer.ParseDateTime(str) : str.EqualsIgnoreCase(bool.TrueString) ? true : str.EqualsIgnoreCase(bool.FalseString) ? false : str == "null" ? (object)null : str;
// Perform the binary operation. public override bool TryBinaryOperation( BinaryOperationBinder binder, object arg, out object result) { // The Textual property contains the textual representaion // of two numbers, in addition to the name // of the binary operation. string resultTextual = dictionary["Textual"].ToString() + " " + binder.Operation + " " + ((DynamicNumber)arg).dictionary["Textual"].ToString(); int resultNumeric; // Checking what type of operation is being performed. switch (binder.Operation) { // Proccessing mathematical addition (a + b). case ExpressionType.Add: resultNumeric = (int)dictionary["Numeric"] + (int)((DynamicNumber)arg).dictionary["Numeric"]; break; // Processing mathematical substraction (a - b). case ExpressionType.Subtract: resultNumeric = (int)dictionary["Numeric"] - (int)((DynamicNumber)arg).dictionary["Numeric"]; break; // In case of any other binary operation, // print out the type of operation and return false, // which means that the language should determine // what to do. // (Usually the language just throws an exception.) default: Console.WriteLine( binder.Operation + ": This binary operation is not implemented"); result = null; return(false); } dynamic finalResult = new DynamicNumber(); finalResult.Textual = resultTextual; finalResult.Numeric = resultNumeric; result = finalResult; return(true); }
public virtual AttributeValue ToAttributeValue(IPocoDynamo db, Type fieldType, string dbType, object value) { var attrVal = ToAttributeValueFn?.Invoke(fieldType, value); if (attrVal != null) { return(attrVal); } if (value == null) { return new AttributeValue { NULL = true } } ; var valueConverter = GetValueConverter(fieldType); if (valueConverter != null) { return(valueConverter.ToAttributeValue(value, fieldType)); } switch (dbType) { case DynamoType.String: var str = value as string ?? ((value as DateTimeOffset?)?.ToString(CultureInfo.InvariantCulture) ?? value.ToString()); return(str == "" //DynamoDB throws on String.Empty ? new AttributeValue { NULL = true } : new AttributeValue { S = str }); case DynamoType.Number: return(new AttributeValue { N = value is string numStr ? numStr : DynamicNumber.GetNumber(value.GetType()).ToString(value) });
static void Test(string[] args) { // Creating the first dynamic number. dynamic firstNumber = new DynamicNumber(); // Creating properties and setting their values // for the first dynamic number. // The TrySetMember method is called. firstNumber.Textual = "One"; firstNumber.Numeric = 1; // Printing out properties. The TryGetMember method is called. Console.WriteLine( firstNumber.Textual + " " + firstNumber.Numeric); // Creating the second dynamic number. dynamic secondNumber = new DynamicNumber(); secondNumber.Textual = "Two"; secondNumber.Numeric = 2; Console.WriteLine( secondNumber.Textual + " " + secondNumber.Numeric); dynamic resultNumber = new DynamicNumber(); // Adding two numbers. The TryBinaryOperation is called. resultNumber = firstNumber + secondNumber; Console.WriteLine( resultNumber.Textual + " " + resultNumber.Numeric); // Subtracting two numbers. TryBinaryOperation is called. resultNumber = firstNumber - secondNumber; Console.WriteLine( resultNumber.Textual + " " + resultNumber.Numeric); // The following statement produces a run-time exception // because the multiplication operation is not implemented. // resultNumber = firstNumber * secondNumber; }
// Perform the unary operation. public override bool TryUnaryOperation( UnaryOperationBinder binder, out object result) { // The Textual property contains // the name of the unary operation in addition // to the textual representaion of the number. string resultTextual = binder.Operation + " " + dictionary["Textual"].ToString(); int resultNumeric; // Determining what type of operation is being performed. switch (binder.Operation) { case ExpressionType.Negate: resultNumeric = -(int)dictionary["Numeric"]; break; default: // In case of any other unary operation, // print out the type of operation and return false, // which means that the language should determine // what to do. // (Usually the language just throws an exception.) Console.WriteLine( binder.Operation + ": This unary operation is not implemented"); result = null; return(false); } dynamic finalResult = new DynamicNumber(); finalResult.Textual = resultTextual; finalResult.Numeric = resultNumeric; result = finalResult; return(true); }
static void Test(string[] args) { // Creating the first dynamic number. dynamic number = new DynamicNumber(); // Creating properties and setting their values // for the dynamic number. // The TrySetMember method is called. number.Textual = "One"; number.Numeric = 1; // Implicit conversion to integer. int testImplicit = number; // Explicit conversion to string. string testExplicit = (String)number; Console.WriteLine(testImplicit); Console.WriteLine(testExplicit); // The following statement produces a run-time exception // because the conversion to double is not implemented. // double test = number; }
static void Test(string[] args) { // Creating a dynamic object. dynamic number = new DynamicNumber(); // Adding and initializing properties. // The TrySetMember method is called. number.Numeric = 1; number.Textual = "One"; // Printing out the result. // The TryGetMember method is called. Console.WriteLine(number.Numeric + " " + number.Textual); // Invoking an object. // The TryInvoke method is called. number(2, "Two"); Console.WriteLine(number.Numeric + " " + number.Textual); // The following statement produces a run-time exception // because in this example the method invocation // expects two arguments. // number(0); }
public object multiply(object lhs, object rhs) => DynamicNumber.Multiply(lhs, rhs);
public override object Evaluate(object lhs, object rhs) => DynamicNumber.BitwiseXOr(lhs, rhs);
public override object Evaluate(object target) => DynamicNumber.BitwiseNot(target);
public override object Evaluate(object lhs, object rhs) => DynamicNumber.BitwiseRightShift(lhs, rhs);
public void Can_apply_operations_to_strings_containing_numbers() { var result = DynamicNumber.Add("1", "1"); Assert.That(result, Is.EqualTo(2)); }
public override object Evaluate(object lhs, object rhs) => DynamicNumber.Mod(lhs, rhs);
public override object Evaluate(object lhs, object rhs) => DynamicNumber.Divide(lhs, rhs.ConvertTo <double>());
public override object Evaluate(object target) => target == null ? 0 : DynamicNumber.Multiply(target, -1).ConvertTo(target.GetType());
public object subtract(object lhs, object rhs) => DynamicNumber.Subtract(lhs, rhs);
public static T ConvertTo <T>(this object from) { if (from == null) { return(default(T)); } var fromType = from.GetType(); if (fromType == typeof(T)) { return((T)from); } if (fromType.IsValueType || typeof(T).IsValueType) { if (!fromType.IsEnum && !typeof(T).IsEnum) { if (typeof(T) == typeof(char) && from is string s) { return((T)(s.Length > 0 ? (object)s[0] : null)); } if (typeof(T) == typeof(string) && from is char c) { return((T)(object)c.ToString()); } var destNumberType = DynamicNumber.GetNumber(typeof(T)); var value = destNumberType?.ConvertFrom(from); if (value != null) { if (typeof(T) == typeof(char)) { return((T)(object)value.ToString()[0]); } return((T)value); } if (typeof(T) == typeof(string)) { var srcNumberType = DynamicNumber.GetNumber(from.GetType()); if (srcNumberType != null) { return((T)(object)srcNumberType.ToString(from)); } } } return((T)ChangeValueType(from, typeof(T))); } if (typeof(IEnumerable).IsAssignableFrom(typeof(T))) { var listResult = TranslateListWithElements.TryTranslateCollections( fromType, typeof(T), from); return((T)listResult); } var to = typeof(T).CreateInstance <T>(); return(to.PopulateWith(from)); }
private object GetValue(object targetValue, TemplateScopeContext scope) { if (targetValue == null || targetValue == JsNull.Value) { return(JsNull.Value); } var targetType = targetValue.GetType(); try { if (!Computed) { if (Property is JsIdentifier identifier) { var ret = PropValue(targetValue, targetType, identifier.Name); // Don't emit member expression on null KeyValuePair if (ret == null && targetType.Name == "KeyValuePair`2") { return(JsNull.Value); } return(ret); } } else { var indexValue = Property.Evaluate(scope); if (indexValue == null) { return(JsNull.Value); } if (targetType.IsArray) { var array = (Array)targetValue; if (indexValue is long l) { return(array.GetValue(l)); } var intValue = indexValue.ConvertTo <int>(); return(array.GetValue(intValue)); } if (targetValue is IDictionary dict) { var ret = dict[indexValue]; return(ret ?? JsNull.Value); } if (indexValue is string propName) { return(PropValue(targetValue, targetType, propName)); } if (targetValue is IList list) { var intValue = indexValue.ConvertTo <int>(); return(list[intValue]); } if (targetValue is IEnumerable e) { var intValue = indexValue.ConvertTo <int>(); var i = 0; foreach (var item in e) { if (i++ == intValue) { return(item); } } return(null); } if (DynamicNumber.IsNumber(indexValue.GetType())) { var indexerMethod = targetType.GetInstanceMethod("get_Item"); if (indexerMethod != null) { var fn = indexerMethod.GetInvoker(); var ret = fn(targetValue, indexValue); return(ret ?? JsNull.Value); } } } } catch (KeyNotFoundException) { return(JsNull.Value); } catch (Exception ex) { var exResult = scope.PageResult.Format.OnExpressionException(scope.PageResult, ex); if (exResult != null) { return(exResult); } var expr = ToRawString(); throw new BindingExpressionException($"Could not evaluate expression '{expr}'", null, expr, ex); } throw new NotSupportedException($"'{targetValue.GetType()}' does not support access by '{Property}'"); }
public object add(object lhs, object rhs) => DynamicNumber.Add(lhs, rhs);
public override object Evaluate(object lhs, object rhs) => DynamicNumber.Subtract(lhs, rhs);