コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the TypeSerializationInfo class.
        /// </summary>
        /// <param name="type">Type info</param>
        public TypeSerializationInfo(Type type)
        {
            object[] attributes = type.GetCustomAttributes(typeof(SerializableClassAttribute), true);

            //  Check input parameters
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            else if (type.IsInterface)
            {
                throw new ArgumentException("Type cannot be an interface", "type");
            }

            this.type = type;
            this.versionSerializable = type.GetInterface(typeof(IVersionSerializable).Name) != null;

            //  Cache whether this type supports ISerializationInfo for forward-compatibility
            //  Note that this only indicates if this specific type supports ISerializationInfo
            //  Forward-compatibility will still be supported if this or any derived classes
            //  implement the necessary interface
            this.supportsSerializationInfo = type.GetInterface(typeof(ISerializationInfo).Name) != null;

            attributes = type.GetCustomAttributes(typeof(SerializableClassAttribute), true);

            deferredInitializationAction = () =>
            {
                if (attributes.Length > 0)
                {
                    this.attribute = attributes[0] as SerializableClassAttribute;
                    InitializeAutoSerializable();
                }
                else
                {
                    InitializeLegacySerializable();
                }
            };
        }
コード例 #2
0
 public static bool IsSerializable(Type t)
 {
     return((t.GetInterface(typeof(ICustomSerializable).Name) != null) ||
            SerializableClassAttribute.HasAttribute(t));
 }
コード例 #3
0
ファイル: BarfFormatter.cs プロジェクト: wilson0x4d/DataRelay
 /// <summary>
 /// Determines whether the specified type can be
 /// serialized and deserialized with this class.
 /// </summary>
 /// <param name="type">The type to serialize/deserialize.</param>
 /// <returns>
 ///     <see langword="true"/> if the specified type is serializable; otherwise, <see langword="false"/>.
 /// </returns>
 public static bool IsSerializable(Type type)
 {
     return(SerializableClassAttribute.HasAttribute(type));
 }
コード例 #4
0
        void InitializeAutoSerializable()
        {
            List <PropertySerializationInfo> properties = new List <PropertySerializationInfo>();
            int obsoleteVersion = 0;

            //  Check if we need to automatically serialize base class members. If so then find
            //  the first base class with the SerializableClass attribute and save its serialization
            //  info for quick access
            if (this.Attribute.SerializeBaseClass == true)
            {
                if (this.Attribute.Inline)
                {
                    throw new ApplicationException("SerializableClass: Inline option cannot be used with the SerializeBase option");
                }

                for (
                    Type baseType = this.type.BaseType;
                    baseType != null;
                    baseType = baseType.BaseType
                    )
                {
                    if (SerializableClassAttribute.HasAttribute(baseType))
                    {
                        this.serializableBaseType = baseType;
                        break;
                    }
                }
            }

            //  Create an ordered list of serializable properties. Get the version from the last
            //  property in the ordered list. Note that in the case of a hierarchy this typeinfo
            //  only serializes the uninherited properties. Base classes will be serialized
            //  before this class using their own serialize/deserialize methods
            BuildPropertyList(properties, out obsoleteVersion);

            if (properties.Count > 0)
            {
                properties.Sort(this);
                this.currentVersion = Math.Max(properties[properties.Count - 1].Version, obsoleteVersion);

                if (this.IsInline && (this.currentVersion > 1))
                {
                    throw new NotSupportedException(string.Format("All properties for inline classes must be version 1 ({0})", this.Type.FullName));
                }

                DumpProperties(properties);
            }

            //  If legacy version is set then all properties must have a higher version
            if (this.LegacyVersion > 0)
            {
                PropertySerializationInfo badProp = null;

                badProp = properties.Find(p => p.Version <= this.LegacyVersion);
                if (badProp != null)
                {
                    throw new InvalidOperationException(string.Format(
                                                            "Class {0} with legacy version {1} contains property {2} with version {3}",
                                                            new object[] {
                        this.Type.Name,
                        this.LegacyVersion,
                        badProp.Name,
                        badProp.Version
                    }));
                }
            }

            //  If MinVersion is higher than the property versions, then the "current" version
            //  is equal to the minimum version. Developers can use this to prevent issues with
            //  breaking changes that don't add new properties. For example, if the base class
            //  hierarchy is changed in some way then the developer must use this to increase the
            //  class' version and prevent the serializaer from loading older versions.
            //
            //  Workitems:
            //  Sprint Backlog Item 40877: Serialization: Allow MinVersion to be higher than property versions
            //
            if (this.MinVersion > this.currentVersion)
            {
                this.currentVersion = this.MinVersion;
            }

            CreateSerializationMethod(properties);
            CreateDeserializationMethod(properties);
            CreateCompareMethod(properties);
        }