Beispiel #1
0
        void ISerializableTypeVisitor.VisitDeserializerMethod(DeserializerAttribute attribute, MethodInfo methodInfo)
        {
            if (attribute.ForAllDescendants)
            {
                if (attribute.TargeType == null)
                {
                    throw Exceptions.DeserializerAttributeTargetTypeMustBeSpecifiedForAllDescendants(attribute, methodInfo);
                }

                if (!IsCorrectSignatureForCustomDeserializerForAllDescendants(methodInfo))
                {
                    throw Exceptions.InvalidDeserializerMethodSignatureForAllDescendants(attribute, methodInfo);
                }
            }
            else
            {
                if (!attribute.Version.HasValue && attribute.TargeType == null)
                {
                    throw Exceptions.CustomDeserializerMustSpecifyVersion(attribute, methodInfo);
                }

                if (!IsCorrectSignatureForCustomDeserializer(methodInfo))
                {
                    throw Exceptions.InvalidDeserializerMethodSignature(attribute, methodInfo);
                }
            }

            if (!methodInfo.IsStatic)
            {
                var defaultPublicConstructor = methodInfo.DeclaringType.GetConstructor(Type.EmptyTypes);
                if (defaultPublicConstructor == null)
                {
                    throw Exceptions.TypeHasNoPublicDefaultConstructor(methodInfo.DeclaringType);
                }
            }

            if (attribute.TargeType == null || attribute.TargeType.IsConcreteType())
            {
                var version = GetVersionForCustomDeserializer(attribute, attribute.TargeType);
                _deserializers.Add(new CustomDeserializer(attribute.PackformatName, version, methodInfo, CustomSerializerCreationReason.Explicit));
            }

            if (attribute.ForAllDescendants)
            {
                //defer the search for descendants to the end of the exploring process
                Action <IEnumerable <Type> > action = visitedTypes =>
                {
                    var descendantTypes = GetAllDescendants(attribute.TargeType, visitedTypes);
                    foreach (var descendantType in descendantTypes.Where(i => i.IsConcreteType()))
                    {
                        var descendantVersion = GetVersionForCustomDeserializer(attribute, descendantType);
                        _deserializers.Add(new CustomDeserializer(descendantType.GetPrettyName(), descendantVersion,
                                                                  methodInfo,
                                                                  CustomSerializerCreationReason.ImplicitByBaseType, descendantType));
                    }
                };
                _deferredActions.Add(action);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Gets the deserializer from the property attribute
        /// </summary>
        /// <param name="property">The property</param>
        /// <returns>The instance of a deserializer</returns>
        public static object GetDeserializerFromAttribute(this PropertyInfo property)
        {
            DeserializerAttribute attribute = property.GetCustomAttribute <DeserializerAttribute>();
            object deserializer;

            if (attribute != null)
            {
                deserializer = attribute.CreateSettingDeserializer() ?? Activator.CreateInstance(attribute.DeserializerType);
            }
            else
            {
                deserializer = null;
            }

            return(deserializer);
        }
Beispiel #3
0
 private static uint GetVersionForCustomDeserializer(DeserializerAttribute attribute, Type targetType)
 {
     return(attribute.Version.HasValue
         ? attribute.Version.Value
         : new TypeInspector(targetType).Version);
 }
Beispiel #4
0
 public static Exception CustomDeserializerMustSpecifyVersion(DeserializerAttribute attribute, MethodInfo methodInfo)
 {
     return(SafeCreateException(() => new ShapeshifterException(CustomDeserializerMustSpecifyVersionId,
                                                                String.Format("Custom deserializer {0}.{1}  must specify version.",
                                                                              methodInfo.DeclaringType == null ? null : methodInfo.DeclaringType.FullName, methodInfo.Name))));
 }
Beispiel #5
0
 public static Exception InvalidDeserializerMethodSignatureForAllDescendants(DeserializerAttribute attribute, MethodInfo methodInfo)
 {
     return(SafeCreateException(() => new ShapeshifterException(InvalidDeserializerMethodSignatureForAllDescendantsId,
                                                                String.Format("Deserializer method {0}.{1} must be Func<object,IShapeshifterReader,Type> because ForAllDescendants is set to true.",
                                                                              methodInfo.DeclaringType == null ? null : methodInfo.DeclaringType.FullName, methodInfo.Name))));
 }
Beispiel #6
0
 public static Exception DeserializerAttributeTargetTypeMustBeSpecifiedForAllDescendants(DeserializerAttribute attribute, MethodInfo methodInfo)
 {
     return(SafeCreateException(() => new ShapeshifterException(DeserializerAttributeTargetTypeMustBeSpecifiedForAllDescendantsId,
                                                                String.Format("DeserializerAttribute on method {0}.{1} must be specified with Type (instead of PackformatName) because ForAllDescendants is set to true.",
                                                                              methodInfo.DeclaringType == null ? null : methodInfo.DeclaringType.FullName, methodInfo.Name))));
 }