コード例 #1
0
        public GenDeserializeContext(
            MethodGenerator generator,
            PartDefinition part,
            IExpression instance,
            IExpression args,
            IVariable header)
            : base(generator)
        {
            ArgumentAssert.IsNotNull(generator, "generator");
            ArgumentAssert.IsNotNull(part, "part");
            ArgumentAssert.IsNotNull(instance, "instance");
            ArgumentAssert.IsNotNull(args, "args");
            ArgumentAssert.IsNotNull(header, "header");

            DeserializationArgs = args;
            Reader   = args.Copy().AddMember("Reader");
            Instance = instance;
            Member   = part.IsBaseType
                                ? instance
                       .MakeReadOnly()
                                : instance
                       .Copy()
                       .AddMember(part.Member);
            IsBaseType = part.IsBaseType;
            Header     = header;
        }
コード例 #2
0
 public GenAssertAreEqualContext(
     MethodGenerator generator,
     PartDefinition definition,
     IExpression expected,
     IExpression actual,
     IExpression assertArgs)
     : base(generator)
 {
     Part = definition;
     _shortcutStack.Push(new KeyValuePair <IExpression, IExpression>(
                             expected,
                             actual));
     AssertArgs = assertArgs;
 }
コード例 #3
0
        public GenFillContext(MethodGenerator generator, IExpression memberShortcut, PartDefinition part)
            : base(generator)
        {
            _fillArgs = generator.CreateExpression(generator.GetParameter(1));

            _members.Push(memberShortcut);
            Part = part;

            NextIsNull = _fillArgs
                         .Copy()
                         .AddMember("NextIsNull");

            NextCollectionSize = _fillArgs
                                 .Copy()
                                 .AddMember("NextCollectionSize");
        }
コード例 #4
0
        public GenSerializeContext(
            MethodGenerator generator,
            PartDefinition part,
            IExpression instance)
            : base(generator)
        {
            ArgumentAssert.IsNotNull(generator, "generator");
            ArgumentAssert.IsNotNull(part, "part");
            ArgumentAssert.IsNotNull(instance, "instance");

            Writer   = generator.CreateExpression(generator.GetParameter(_serializationArgsIndex)).AddMember("Writer");
            Instance = instance;
            Member   = part.IsBaseType
                                ? instance
                       .MakeReadOnly()
                                : instance
                       .Copy()
                       .AddMember(part.Member);
        }
コード例 #5
0
        public IPartBuilder GetPartBuilder(Type type, PartDefinition partDefinition)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            IPartBuilder result;

            if (partDefinition.ReadMethod != null || partDefinition.WriteMethod != null)
            {
                return(CustomPartBuilder.Create(type, partDefinition));
            }

            if (type.IsGenericParameter)
            {
                return(new DeferredPartBuilder(type, partDefinition.Flags));
            }

            if (_readyParts.TryGetValue(type, out result))
            {
                return(result);
            }

            if (type.IsArray)
            {
                return(new ArrayPartBuilder(type, partDefinition, this));
            }

            if (type.IsEnum)
            {
                return(new EnumPartBuilder(type, partDefinition, this));
            }

            if (BarfFormatter.IsSerializable(type))
            {
                return(new DeferredPartBuilder(type, partDefinition.Flags));
            }

            if (type.IsGenericType)
            {
                var genericTypeDef = type.GetGenericTypeDefinition();
                PartBuilderFactory factory;
                if (_genericParts.TryGetValue(genericTypeDef, out factory))
                {
                    return(factory(type, partDefinition));
                }
            }

            if (partDefinition.IsDynamic)
            {
                return(new DeferredPartBuilder(type, partDefinition.Flags));
            }

            if (typeof(IVersionSerializable).IsAssignableFrom(type))
            {
                return(new VersionSerializablePartBuilder(type));
            }

            if (typeof(ICustomSerializable).IsAssignableFrom(type))
            {
                return(new CustomSerializablePartBuilder(type));
            }

            var builder = SerializableStructPartBuilder.TryCreate(type, partDefinition, this);

            if (builder != null)
            {
                return(builder);
            }

            if (type.IsSerializable && !partDefinition.IsBaseType)
            {
                return(BinaryFormatterPartBuilder.Instance);
            }

            throw new NotSupportedException("The defined member cannot be serialized because it and/or one if its nested types are not serializable - " + partDefinition);
        }
コード例 #6
0
        private static BarfTypeDefinition Create(Type type)
        {
            var parts  = new List <PartDefinition>();
            var errors = new List <string>();
            var result = new BarfTypeDefinition
            {
                Type   = type,
                Errors = new ReadOnlyCollection <string>(errors),
                Parts  = new ReadOnlyCollection <PartDefinition>(parts)
            };

            try
            {
                var classAttribute = (SerializableClassAttribute)Attribute.GetCustomAttribute(type, typeof(SerializableClassAttribute), false);

                if (classAttribute == null)
                {
                    errors.Add(type.Name + " does not define a " + typeof(SerializableAttribute).Name);
                    return(result);
                }

                var typePartDef = PartDefinition.Create(type);

                if (typePartDef != null)
                {
                    parts.Add(typePartDef);
                }

                const BindingFlags flags
                    = BindingFlags.Instance
                      | BindingFlags.Public
                      | BindingFlags.NonPublic
                      | BindingFlags.DeclaredOnly;

                parts.AddRange(PartDefinition.CreateInheritedParts(type));

                foreach (var member in type.GetMembers(flags))
                {
                    if (member.DeclaringType != type)
                    {
                        continue;
                    }
                    if (member.MemberType != MemberTypes.Property && member.MemberType != MemberTypes.Field)
                    {
                        continue;
                    }

                    var partDefinition = PartDefinition.Create(member);

                    if (partDefinition == null)
                    {
                        continue;
                    }

                    if (partDefinition.IsDynamic)
                    {
                        if (partDefinition.Type.IsValueType)
                        {
                            // value types may not be dynamic
                            return(result);
                        }
                    }

                    parts.Add(partDefinition);
                }

                parts.Sort(BarfPartInfoComparer.Default);

                result.CurrentVersion = result.Parts
                                        .Select(part => part.Version)
                                        .Concat(new[] { classAttribute.MinVersion })
                                        .Max <int>();


                // before we would guess the min version if it was zero
                // by taking the lowest property version. However
                // if an empty class defining no parts will be version
                // zero. If a property is added at version 1 min version
                // will then become 1 making it impossible to deserialize
                // the older empty class streams.
                result.MinVersion = classAttribute.MinVersion;

                result.MinDeserializeVersion = Math.Max(classAttribute.MinDeserializeVersion, 0);

                result.LegacyVersion = classAttribute.LegacyVersion;

                if (result.LegacyVersion >= 0)
                {
                    var method = type.ResolveMethod("Deserialize", typeof(IPrimitiveReader), typeof(int));
                    if (method != null && method.IsStatic)
                    {
                        method = null;
                    }
                    if (method == null && result.LegacyVersion > 0)
                    {
                        errors.Add("LegacyVersion=" + result.LegacyVersion + " but no method void Deserialize(IPrimitiveReader, int) was defined.");
                    }
                    else
                    {
                        result.LegacyDeserializeMethod = method;
                    }
                }

                result.IsForwardCompatible = typeof(ISerializationInfo).IsAssignableFrom(type);
            }
            catch (Exception ex)
            {
                errors.Add(ex.ToString());
                return(result);
            }
            return(result);
        }
コード例 #7
0
        public static IPartBuilder GetPartBuilder(this IPartResolver resolver, Type type, PartDefinition partDefinition, bool throwIfUnresolvable)
        {
            var result = resolver.GetPartBuilder(type, partDefinition);

            if (result == null && throwIfUnresolvable)
            {
                throw new NotSupportedException(string.Format(
                                                    "Property - {0} cannot be serialized by the auto-serialization framework. PartResolver - {1}",
                                                    partDefinition,
                                                    resolver));
            }

            return(result);
        }
コード例 #8
0
 public static IPartBuilder GetPartBuilder(this IPartResolver resolver, PartDefinition partDefinition, bool throwIfUnresolvable)
 {
     return(resolver.GetPartBuilder(partDefinition.Type, partDefinition, throwIfUnresolvable));
 }
コード例 #9
0
 public static IPartBuilder GetPartBuilder(this IPartResolver resolver, PartDefinition partDefinition)
 {
     return(resolver.GetPartBuilder(partDefinition.Type, partDefinition));
 }
コード例 #10
0
 public static IPartBuilder GetCurrentBuilder(this PartDefinition definition)
 {
     return(PartResolver.Current.GetPartBuilder(definition));
 }