Ejemplo n.º 1
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 static bool TryConstructFromData <TDstValue, TSrcValue>(ref TSrcValue srcValue, string typeIdentifierKey, VisitResult result, out TDstValue dstValue)
        {
            if (typeof(UnityEngine.Object).IsAssignableFrom(typeof(TDstValue)))
            {
                dstValue = default;
                return(false);
            }

            // Try to construct based on the source type.
            if (TypeConstruction.TryConstruct(srcValue.GetType(), out dstValue))
            {
                return(true);
            }

            // Try to construct based on the destination type.
            if (TypeConstruction.TryConstruct(out dstValue))
            {
                return(true);
            }

            // If type identifier key option is not set, cannot construct this type.
            if (string.IsNullOrEmpty(typeIdentifierKey))
            {
                return(false);
            }

            // Try to get destination type name from type identifier meta data.
            if (!PropertyContainer.TryGetValue(ref srcValue, typeIdentifierKey, out string assemblyQualifiedTypeName))
            {
                result.AddLog($"PropertyContainer.Construct failed to construct DstType=[{typeof(TDstValue)}]. SrcValue Property=[{typeIdentifierKey}] was not found.");
                return(false);
            }

            // Verify destination type name is valid.
            if (string.IsNullOrEmpty(assemblyQualifiedTypeName))
            {
                result.AddException(new InvalidOperationException($"PropertyContainer.Construct failed to construct DstType=[{typeof(TDstValue)}]. SrcValue Property=[{typeIdentifierKey}] contained null or empty type information."));
                return(false);
            }

            // Try to get destination type.
            var dstType = Type.GetType(assemblyQualifiedTypeName);

            if (null == dstType)
            {
                result.AddException(new InvalidOperationException($"PropertyContainer.Construct failed to construct DstType=[{typeof(TDstValue)}]. Could not resolve type from TypeName=[{assemblyQualifiedTypeName}]."));
                return(false);
            }

            return(TypeConstruction.TryConstruct(dstType, out dstValue));
        }