public object Operate(object operand) { Ensure.That(nameof(operand)).IsNotNull(operand); var type = operand.GetType(); if (manualHandlers.ContainsKey(type)) { return(manualHandlers[type](operand)); } if (customMethodName != null) { if (!userDefinedOperators.ContainsKey(type)) { var method = type.GetMethod(customMethodName, BindingFlags.Public | BindingFlags.Static); if (method != null) { userDefinedOperandTypes.Add(type, ResolveUserDefinedOperandType(method)); } userDefinedOperators.Add(type, method?.Prewarm()); } if (userDefinedOperators[type] != null) { operand = ConversionUtility.Convert(operand, userDefinedOperandTypes[type]); return(userDefinedOperators[type].Invoke(null, operand)); } } return(CustomHandling(operand)); }
public object Paste(Type type) { if (!CanPaste(type)) { throw new InvalidOperationException($"Graph clipboard does not contain '{type.CSharpName(false)}'."); } return(ConversionUtility.Convert(Paste(), type)); }
private void _Run(object assigner, object assignee) { var oldValue = this.assignee.Get(assignee); var newValue = ConversionUtility.Convert(this.assigner.Invoke(assigner), this.assignee.type); this.assignee.Set(assignee, newValue); if (!Equals(oldValue, newValue)) { if (assigner is IAssigner _assigner) { _assigner.ValueChanged(); } } }
private void EnforceType() { if (metadata.value?.GetType() == type) { return; } metadata.UnlinkChildren(); if (type == null) { metadata.value = null; } else if (ConversionUtility.CanConvert(metadata.value, type, true)) { metadata.value = ConversionUtility.Convert(metadata.value, type); } else { metadata.value = type.TryInstantiate(); } metadata.InferOwnerFromParent(); }
public virtual object Operate(object leftOperand, object rightOperand) { OperatorQuery query; var leftType = leftOperand?.GetType(); var rightType = rightOperand?.GetType(); if (leftType != null && rightType != null) { query = new OperatorQuery(leftType, rightType); } else if (leftType != null && leftType.IsNullable()) { query = new OperatorQuery(leftType, leftType); } else if (rightType != null && rightType.IsNullable()) { query = new OperatorQuery(rightType, rightType); } else if (leftType == null && rightType == null) { return(BothNullHandling()); } else { return(SingleNullHandling()); } if (handlers.ContainsKey(query)) { return(handlers[query](leftOperand, rightOperand)); } if (customMethodName != null) { if (!userDefinedOperators.ContainsKey(query)) { var leftMethod = query.leftType.GetMethod(customMethodName, BindingFlags.Public | BindingFlags.Static, null, new[] { query.leftType, query.rightType }, null); if (query.leftType != query.rightType) { var rightMethod = query.rightType.GetMethod(customMethodName, BindingFlags.Public | BindingFlags.Static, null, new[] { query.leftType, query.rightType }, null); if (leftMethod != null && rightMethod != null) { throw new AmbiguousOperatorException(symbol, query.leftType, query.rightType); } var method = (leftMethod ?? rightMethod); if (method != null) { userDefinedOperandTypes.Add(query, ResolveUserDefinedOperandTypes(method)); } userDefinedOperators.Add(query, method?.Prewarm()); } else { if (leftMethod != null) { userDefinedOperandTypes.Add(query, ResolveUserDefinedOperandTypes(leftMethod)); } userDefinedOperators.Add(query, leftMethod?.Prewarm()); } } if (userDefinedOperators[query] != null) { leftOperand = ConversionUtility.Convert(leftOperand, userDefinedOperandTypes[query].leftType); rightOperand = ConversionUtility.Convert(rightOperand, userDefinedOperandTypes[query].rightType); return(userDefinedOperators[query].Invoke(null, leftOperand, rightOperand)); } } return(CustomHandling(leftOperand, rightOperand)); }
public static object GetValue(this IGettable gettable, Type type) { return(ConversionUtility.Convert(gettable.GetValue(), type)); }