public void VisitProperty <TSourceProperty, TSourceValue>(TSourceProperty srcProperty, ref TSourceContainer srcContainer, ref ChangeTracker propertyChangeTracker)
                where TSourceProperty : IProperty <TSourceContainer, TSourceValue>
            {
                var srcValue = srcProperty.GetValue(ref srcContainer);

                if (TypeConversion.TryConvert <TSourceValue, TDestinationValue>(srcValue, out var dstValue))
                {
                    if (CustomEquality.Equals(dstValue, DstProperty.GetValue(ref DstContainer)))
                    {
                        return;
                    }

                    DstProperty.SetValue(ref DstContainer, dstValue);
                    propertyChangeTracker.IncrementVersion <TDestinationProperty, TDestinationContainer, TDestinationValue>(DstProperty, ref DstContainer);
                    return;
                }

                if (DstProperty.IsContainer)
                {
                    var changeTracker = new ChangeTracker(propertyChangeTracker.VersionStorage);

                    dstValue = DstProperty.GetValue(ref DstContainer);

                    PropertyContainer.Transfer(ref dstValue, ref srcValue, ref changeTracker);

                    DstProperty.SetValue(ref DstContainer, dstValue);

                    if (changeTracker.IsChanged())
                    {
                        propertyChangeTracker.IncrementVersion <TDestinationProperty, TDestinationContainer, TDestinationValue>(DstProperty, ref DstContainer);
                    }
                }
            }
Example #2
0
                    public void VisitProperty <TDstElementProperty, TDstElementValue>(
                        TDstElementProperty dstElementProperty,
                        ref TDstContainer dstContainer,
                        ref ChangeTracker changeTracker)
                        where TDstElementProperty : ICollectionElementProperty <TDstContainer, TDstElementValue>
                    {
                        if (dstElementProperty.IsReadOnly)
                        {
                            return;
                        }

                        if (!RuntimeTypeInfoCache <TSrcElementValue> .IsValueType() && null == SrcElementValue)
                        {
                            dstElementProperty.SetValue(ref dstContainer, default);
                        }
                        else if (TypeConversion.TryConvert <TSrcElementValue, TDstElementValue>(SrcElementValue, out var dstElementValue))
                        {
                            dstElementProperty.SetValue(ref dstContainer, dstElementValue);
                        }
                        else if (dstElementProperty.IsContainer)
                        {
                            dstElementValue = dstElementProperty.GetValue(ref dstContainer);

                            if (RuntimeTypeInfoCache <TDstElementValue> .IsValueType() || null != dstElementValue)
                            {
                                Transfer(ref dstElementValue, ref SrcElementValue, Result);
                            }

                            dstElementProperty.SetValue(ref dstContainer, dstElementValue);
                        }
                        else
                        {
                            Result.AddLog($"PropertyContainer.Transfer ContainerType=[{typeof(TDstContainer)}] PropertyName=[{dstElementProperty.GetName()}] could not be transferred.");
                        }
                    }
                public void VisitProperty <TDestinationElementProperty, TDestinationElement>(TDestinationElementProperty dstElementProperty, ref TDestinationContainer dstContainer, ref ChangeTracker propertyChangeTracker)
                    where TDestinationElementProperty : ICollectionElementProperty <TDestinationContainer, TDestinationElement>
                {
                    if (TypeConversion.TryConvert <TSourceElement, TDestinationElement>(SrcElementValue, out var dstElementValue))
                    {
                        if (CustomEquality.Equals(dstElementValue, dstElementProperty.GetValue(ref dstContainer)))
                        {
                            return;
                        }

                        dstElementProperty.SetValue(ref dstContainer, dstElementValue);
                        propertyChangeTracker.IncrementVersion <TDestinationElementProperty, TDestinationContainer, TDestinationElement>(dstElementProperty, ref dstContainer);
                        return;
                    }

                    if (dstElementProperty.IsContainer)
                    {
                        var changeTracker = new ChangeTracker(propertyChangeTracker.VersionStorage);

                        dstElementValue = dstElementProperty.GetValue(ref dstContainer);

                        PropertyContainer.Transfer(ref dstElementValue, ref SrcElementValue, ref changeTracker);

                        dstElementProperty.SetValue(ref dstContainer, dstElementValue);

                        if (changeTracker.IsChanged())
                        {
                            propertyChangeTracker.IncrementVersion <TDestinationElementProperty, TDestinationContainer, TDestinationElement>(dstElementProperty, ref dstContainer);
                        }
                    }
                }
Example #4
0
        static VisitErrorCode VisitSetValueProperty <TContainer, TProperty, TPropertyValue, TTargetValue>(
            TProperty property,
            ref TContainer container, PropertyPath propertyPath, int propertyPathIndex, TTargetValue value,
            ref ChangeTracker changeTracker)
            where TProperty : IProperty <TContainer, TPropertyValue>
        {
            if (propertyPathIndex < propertyPath.PartsCount - 1)
            {
                var sub    = property.GetValue(ref container);
                var status = TrySetValueImpl(ref sub, propertyPath, propertyPathIndex + 1, value, ref changeTracker);
                if (status == VisitErrorCode.Ok)
                {
                    property.SetValue(ref container, sub);
                }

                return(status);
            }

            if (TypeConversion.TryConvert(value, out TPropertyValue convertedValue))
            {
                property.SetValue(ref container, convertedValue);
                return(VisitErrorCode.Ok);
            }

            return(VisitErrorCode.InvalidCast);
        }
 protected override void VisitPath <TContainer, TValue>(Property <TContainer, TValue> property,
                                                        ref TContainer container, ref TValue value)
 {
     if (!TypeConversion.TryConvert(value, out Value))
     {
         ErrorCode = VisitErrorCode.InvalidCast;
     }
 }
Example #6
0
 public void VisitProperty <TProperty, TSourceValue>(TProperty property, ref TContainer container, ref ChangeTracker changeTracker)
     where TProperty : IProperty <TContainer, TSourceValue>
 {
     if (!TypeConversion.TryConvert(property.GetValue(ref container), out DstValue))
     {
         Result = k_ResultErrorConvert;
     }
 }
Example #7
0
 protected override void VisitPath <TContainer, TValue>(Property <TContainer, TValue> property,
                                                        ref TContainer container, ref TValue value)
 {
     if (TypeConversion.TryConvert(Value, out TValue v))
     {
         property.SetValue(ref container, v);
     }
     else
     {
         ErrorCode = VisitErrorCode.InvalidCast;
     }
 }
Example #8
0
        public static TValue GetValueOrDefault <TValue>(this IPropertyContainer container, string name, TValue defaultValue = default(TValue))
        {
            var property = (container.PropertyBag.FindProperty(name) as IValueProperty);

            if (null == property)
            {
                return(defaultValue);
            }

            var    value = property?.GetObjectValue(container);
            TValue result;

            return(TypeConversion.TryConvert(value, out result) ? result : defaultValue);
        }
Example #9
0
            public void VisitProperty <TProperty, TDestinationValue>(TProperty property, ref TContainer container, ref ChangeTracker changeTracker)
                where TProperty : IProperty <TContainer, TDestinationValue>
            {
                if (!TypeConversion.TryConvert <TSourceValue, TDestinationValue>(SrcValue, out var dstValue))
                {
                    Result = k_ResultErrorConvert;
                    return;
                }

                if (CustomEquality.Equals(dstValue, property.GetValue(ref container)))
                {
                    return;
                }

                property.SetValue(ref container, dstValue);
                changeTracker.IncrementVersion <TProperty, TContainer, TDestinationValue>(property, ref container);
            }
        static VisitErrorCode VisitGetValueProperty <TContainer, TProperty, TValue, TTargetValue>(TProperty property,
                                                                                                  ref TContainer container, PropertyPath propertyPath, int propertyPathIndex, ref ChangeTracker changeTracker, out TTargetValue value)
            where TProperty : IProperty <TContainer, TValue>
        {
            if (propertyPathIndex < propertyPath.PartsCount - 1)
            {
                var sub = property.GetValue(ref container);
                return(TryGetValueImpl(ref sub, propertyPath, propertyPathIndex + 1, ref changeTracker, out value));
            }

            if (TypeConversion.TryConvert(property.GetValue(ref container), out value))
            {
                return(VisitErrorCode.Ok);
            }

            return(VisitErrorCode.InvalidCast);
        }
Example #11
0
            protected override void VisitPath <TContainer, TValue>(Property <TContainer, TValue> property,
                                                                   ref TContainer container, ref TValue value)
            {
                if (property.IsReadOnly)
                {
                    ErrorCode = VisitErrorCode.AccessViolation;
                    return;
                }

                if (TypeConversion.TryConvert(Value, out TValue v))
                {
                    property.SetValue(ref container, v);
                }
                else
                {
                    ErrorCode = VisitErrorCode.InvalidCast;
                }
            }
        static VisitErrorCode VisitCollectionGetValueProperty <TContainer, TProperty, TValue, TTargetValue>(
            TProperty property, ref TContainer container, PropertyPath propertyPath, int propertyPathIndex,
            ref ChangeTracker changeTracker, out TTargetValue value)
            where TProperty : ICollectionProperty <TContainer, TValue>
        {
            if (propertyPathIndex < propertyPath.PartsCount - 1 || propertyPath[propertyPathIndex].IsListItem)
            {
                var callback = new GetCollectionItemGetter <TContainer, TTargetValue>(propertyPath, propertyPathIndex);
                property.GetPropertyAtIndex(ref container, propertyPath[propertyPathIndex].Index, ref changeTracker,
                                            ref callback);
                value = callback.Value;
                return(callback.ErrorCode);
            }

            if (TypeConversion.TryConvert(property.GetValue(ref container), out value))
            {
                return(VisitErrorCode.Ok);
            }

            return(VisitErrorCode.InvalidCast);
        }
Example #13
0
        static VisitErrorCode VisitCollectionSetValueProperty <TContainer, TProperty, TPropertyValue, TTargetValue>(
            TProperty property, ref TContainer container, PropertyPath propertyPath, int propertyPathIndex,
            TTargetValue value,
            ref ChangeTracker changeTracker)
            where TProperty : ICollectionProperty <TContainer, TPropertyValue>
        {
            if (propertyPathIndex < propertyPath.PartsCount - 1 || propertyPath[propertyPathIndex].IsListItem)
            {
                var getter =
                    new SetCollectionItemGetter <TContainer, TTargetValue>(propertyPath, propertyPathIndex, value);
                property.GetPropertyAtIndex(ref container, propertyPath[propertyPathIndex].Index, ref changeTracker,
                                            ref getter);
                return(getter.ErrorCode);
            }

            if (TypeConversion.TryConvert(value, out TPropertyValue convertedValue))
            {
                property.SetValue(ref container, convertedValue);
                return(VisitErrorCode.Ok);
            }

            return(VisitErrorCode.InvalidCast);
        }