예제 #1
0
        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();
        }
예제 #3
0
        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));
        }
예제 #4
0
 public static object GetValue(this IGettable gettable, Type type)
 {
     return(ConversionUtility.Convert(gettable.GetValue(), type));
 }