private void SetValueType(Binding binding, ValueNode bindingTarget,
                                  ReadOnlyCollection <SubtypeBaseAttribute> attributes,
                                  SubtypeDefaultBaseAttribute defaultAttribute)
        {
            // try to resolve value type using subtype mapping
            var subTypeValue = binding.GetValue(bindingTarget);

            // find matching subtype, if available
            var matchingAttribute = attributes.SingleOrDefault(
                attribute => subTypeValue.Equals(Convert.ChangeType(attribute.Value, subTypeValue.GetType(), null)));

            _valueType = matchingAttribute?.Subtype;

            // we couldn't match so use default if specified
            if (_valueType == null && defaultAttribute != null)
            {
                _valueType = defaultAttribute.Subtype;
            }
        }
Esempio n. 2
0
        private void SetValueType(BindingCollection bindings, ValueNode bindingTarget,
                                  ReadOnlyCollection <SubtypeBaseAttribute> attributes,
                                  Binding subtypeFactoryBinding,
                                  ISubtypeFactory subtypeFactory,
                                  SubtypeDefaultBaseAttribute defaultAttribute)
        {
            if (bindings != null)
            {
                // try to resolve value type using subtype mapping
                var subTypeValue = bindings.GetValue(bindingTarget);

                var toTargetAttributes = attributes.Where(attribute => attribute.BindingMode != BindingMode.OneWayToSource);

                // find matching subtype, if available
                var matchingAttribute = toTargetAttributes.SingleOrDefault(
                    attribute => subTypeValue.Equals(
                        Convert.ChangeType(attribute.Value, subTypeValue.GetType(), null)));

                _valueType = matchingAttribute?.Subtype;
            }

            if (_valueType == null && subtypeFactoryBinding != null)
            {
                var subTypeFactoryValue = subtypeFactoryBinding.GetValue(bindingTarget);

                if (subtypeFactory.TryGetType(subTypeFactoryValue, out var valueType))
                {
                    _valueType = valueType;
                }
            }

            // we couldn't match so use default if specified
            if (_valueType == null && defaultAttribute != null)
            {
                _valueType = defaultAttribute.Subtype;
            }
        }