internal static void Construct <TDstContainer, TSrcContainer>( ref TDstContainer dstContainer, ref TSrcContainer srcContainer, VisitResult result, PropertyContainerConstructOptions options = default) { if (RuntimeTypeInfoCache <TDstContainer> .IsAbstractOrInterface() || typeof(TDstContainer) != dstContainer.GetType()) { var propertyBag = PropertyBagResolver.Resolve(dstContainer.GetType()); var action = new ConstructAbstractType <TSrcContainer> { Options = options, Result = result, SrcContainer = srcContainer, DstContainerBoxed = dstContainer }; propertyBag.Cast(ref action); dstContainer = (TDstContainer)action.DstContainerBoxed; } else { var visitor = new TypeConstructionVisitor <TDstContainer>(dstContainer, result, options); Visit(ref srcContainer, ref visitor); dstContainer = visitor.Target; } }
public static VisitResult Construct <TDstContainer, TSrcContainer>(ref TDstContainer dstContainer, ref TSrcContainer srcContainer, PropertyContainerConstructOptions options = default) { if (!RuntimeTypeInfoCache <TSrcContainer> .IsValueType() && srcContainer == null) { throw new ArgumentNullException(nameof(srcContainer)); } if (!RuntimeTypeInfoCache <TDstContainer> .IsValueType() && dstContainer == null) { if (typeof(UnityEngine.Object).IsAssignableFrom(typeof(TDstContainer))) { throw new ArgumentNullException(nameof(dstContainer)); } if (!TypeConstruction.TryConstruct(srcContainer.GetType(), out dstContainer)) { throw new ArgumentNullException(nameof(dstContainer)); } } var result = VisitResult.GetPooled(); Construct(ref dstContainer, ref srcContainer, result, options); return(result); }
public TransferVisitor(TDstContainer dstContainer, VisitResult result) { m_Result = result; m_DstContainer = dstContainer; m_DstPropertyBag = PropertyBagResolver.Resolve <TDstContainer>(); if (null == m_DstPropertyBag) { throw new ArgumentException($"No property bag exists for the given Type=[{typeof(TDstContainer)}]"); } }
public TypeConstructionVisitor(TDstContainer dstContainer, VisitResult result, PropertyContainerConstructOptions options) { m_Options = options; Result = result; m_DstContainer = dstContainer; m_DstPropertyBag = PropertyBagResolver.Resolve <TDstContainer>(); if (null == m_DstPropertyBag) { throw new ArgumentException($"No property bag exists for the given Type=[{typeof(TDstContainer)}]"); } }
public static VisitResult Transfer <TDstContainer, TSrcContainer>(ref TDstContainer dstContainer, ref TSrcContainer srcContainer) { if (!RuntimeTypeInfoCache <TDstContainer> .IsValueType() && dstContainer == null) { throw new ArgumentNullException(nameof(dstContainer)); } var result = VisitResult.GetPooled(); Transfer(ref dstContainer, ref srcContainer, result); return(result); }
static void Transfer <TDstContainer, TSrcContainer>( ref TDstContainer dstContainer, ref TSrcContainer srcContainer, VisitResult result) { if (RuntimeTypeInfoCache <TDstContainer> .IsAbstractOrInterface() || typeof(TDstContainer) != dstContainer.GetType()) { var propertyBag = PropertyBagResolver.Resolve(dstContainer.GetType()); var action = new TransferAbstractType <TSrcContainer> { Result = result, SrcContainer = srcContainer, DstContainerBoxed = dstContainer }; propertyBag.Cast(ref action); dstContainer = (TDstContainer)action.DstContainerBoxed; } else { var visitor = new TransferVisitor <TDstContainer>(dstContainer, result); Visit(ref srcContainer, ref visitor); dstContainer = visitor.Target; } }
public void TransferEvents(VisitResult result) { m_Events.AddRange(result.m_Events); result.m_Events.Clear(); }
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)); }