System.Version没有序列号 [英] System.Version not serialized

查看:126
本文介绍了System.Version没有序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 System.Version 属性的类,它看起来像这样:




  • 版本

    • 构建:111

    • 专业:1

    • MajorRevision :0

    • 辅修:1

    • MinorRevision:10

    • 修订:10




当我序列化类,版本总是空的:

 <版本/> 



客户端类如下:

  [Serializable接口] 
公共类客户端
{
公共字符串描述;
公共字符串目录;
公众的DateTime RELEASEDATE;
公版版本;
}


解决方案

System.Version 是不是可序列化,如果你看一下它在MSDN 的属性,你会看到他们有没有制定者......所以,串行器不会保存。但是,这种方法仍然有效。那篇文章(老了,但仍然有效)提供了一个版本类是可序列化,你能切换到这一点,得到了?



按tomfanning修改结果
我已经捕捞从死人网站的代码进行archive.org的,转载如下:

 使用系统; 
使用System.Globalization;
命名空间CubicOrange.Version
{
///<总结>
///的System.Version类的序列化版本。
///< /总结>
[Serializable接口]
公共类的ModuleVersion:ICloneable,IComparable的
{
私人诠释专业;
私人诠释未成年人;
私人诠释构建;
私人诠释修改;
///<总结>
///获取重要。
///< /总结>
///< VALUE>< /值>
公众诠释主要
{
得到
{
返回专业;
}

{
大调=价值;
}
}
///<总结>
///获取未成年人。
///< /总结>
///< VALUE>< /值>
公众诠释小调
{
得到
{
返回未成年人;
}

{
小调=价值;
}
}
///<总结>
///获取构建。
///< /总结>
///< VALUE>< /值>
公众诠释构建
{
得到
{
返回构建;
}

{
=构建价值;
}
}
///<总结>
///获取的修订。
///< /总结>
///< VALUE>< /值>
公众诠释版本
{
得到
{
返回修改;
}

{
=修正值;
}
}
///<总结>
///创建一个新的<见CREF =的ModuleVersion/>实例。
///< /总结>
公众的ModuleVersion()
{
this.build = -1;
this.revision = -1;
this.major = 0;
this.minor = 0;
}
///<总结>
///创建一个新的<见CREF =的ModuleVersion/>实例。
///< /总结>
///< PARAM NAME =版本方式>版本< /参数>
公众的ModuleVersion(字符串版本)
{
this.build = -1;
this.revision = -1;
如果(版本== NULL)
{
抛出新的ArgumentNullException(版本);
}
的char [] = chArray1新的char [1] {}'。';
的String [] = textArray1 version.Split(chArray1);
INT NUM1 = textArray1.Length;如果
((NUM1 2)||(NUM1> 4))
{
抛出新的ArgumentException(Arg_VersionString);
}
this.major = int.Parse(textArray1 [0],CultureInfo.InvariantCulture);
如果(this.major℃下)
{
抛出新ArgumentOutOfRangeException(版本,ArgumentOutOfRange_Version);
}
this.minor = int.Parse(textArray1 [1],CultureInfo.InvariantCulture);
如果(this.minor℃下)
{
抛出新ArgumentOutOfRangeException(版本,ArgumentOutOfRange_Version);
}
NUM1 - = 2;
如果(NUM1大于0)
{
this.build = int.Parse(textArray1 [2],CultureInfo.InvariantCulture);
如果(this.build℃下)
{
抛出新ArgumentOutOfRangeException(建,ArgumentOutOfRange_Version);
}
num1--;
如果(NUM1大于0)
{
this.revision = int.Parse(textArray1 [3],CultureInfo.InvariantCulture);
如果(this.revision℃下)
{
抛出新ArgumentOutOfRangeException(修订版,ArgumentOutOfRange_Version);
}
}
}
}
///<总结>
///创建一个新的<见CREF =的ModuleVersion/>实例。
///< /总结>
///< PARAM NAME =主要方式>&主要LT; /参数>
///< PARAM NAME =小方式>&小LT; /参数>
公众的ModuleVersion(INT主要,次要INT)
{
this.build = -1;
this.revision = -1;
如果(主要℃下)
{
抛出新ArgumentOutOfRangeException(重大,ArgumentOutOfRange_Version);
}
如果(次要℃下)
{
抛出新ArgumentOutOfRangeException(小,ArgumentOutOfRange_Version);
}
this.major =专业;
this.minor =轻微;
this.major =专业;
}
///<总结>
///创建一个新的<见CREF =的ModuleVersion/>实例。
///< /总结>
///< PARAM NAME =主要方式>&主要LT; /参数>
///< PARAM NAME =小方式>&小LT; /参数>
///< PARAM NAME =打造>构建与LT; /参数>
公众的ModuleVersion(INT主要,次要INT,INT版本)
{
this.build = -1;
this.revision = -1;
如果(主要℃下)
{
抛出新ArgumentOutOfRangeException(重大,ArgumentOutOfRange_Version);
}
如果(次要℃下)
{
抛出新ArgumentOutOfRangeException(小,ArgumentOutOfRange_Version);
}
如果(建℃下)
{
抛出新ArgumentOutOfRangeException(建,ArgumentOutOfRange_Version);
}
this.major =专业;
this.minor =轻微;
this.build =建立;
}
///<总结>
///创建一个新的<见CREF =的ModuleVersion/>实例。
///< /总结>
///< PARAM NAME =主要方式>&主要LT; /参数>
///< PARAM NAME =小方式>&小LT; /参数>
///< PARAM NAME =打造>构建与LT; /参数>
///< PARAM NAME =修订>修订< /参数>
公众的ModuleVersion(INT主要,次要INT,INT打造,诠释修订)
{
this.build = -1;
this.revision = -1;
如果(主要℃下)
{
抛出新ArgumentOutOfRangeException(重大,ArgumentOutOfRange_Version);
}
如果(次要℃下)
{
抛出新ArgumentOutOfRangeException(小,ArgumentOutOfRange_Version);
}
如果(建℃下)
{
抛出新ArgumentOutOfRangeException(建,ArgumentOutOfRange_Version);
}
如果(修订℃下)
{
抛出新ArgumentOutOfRangeException(修订版,ArgumentOutOfRange_Version);
}
this.major =专业;
this.minor =轻微;
this.build =建立;
this.revision =版本;
}
#地区ICloneable会员
///<总结>
///复制此实例。
///< /总结>
///<&回报GT;< /回报>
公共对象的clone()
{
的ModuleVersion VERSION1 =新的ModuleVersion();
version1.major = this.major;
version1.minor = this.minor;
version1.build = this.build;
version1.revision = this.revision;
返回VERSION1;
}
#endregion
#地区IComparable的会员
///<总结>
///比较。
///< /总结>
///< PARAM NAME =目标文件方式>&的OBJ LT; /参数>
///<&回报GT;< /回报>
公众诠释的CompareTo(对象版本)
{
如果(版本== NULL)
{
返回1;
}
如果((版本的ModuleVersion)!)
{
抛出新的ArgumentException(Arg_MustBeVersion);
}
的ModuleVersion VERSION1 =(的ModuleVersion)版本;
如果(this.major = version1.Major!)
{
如果(this.major> version1.Major)
{
返回1;
}
返回-1;
}
如果(this.minor = version1.Minor!)
{
如果(this.minor> version1.Minor)
{
返回1;
}
返回-1;
}
如果(this.build = version1.Build!)
{
如果(this.build> version1.Build)
{
返回1;
}
返回-1;
}
如果(this.revision == version1.Revision)
{
返回0;
}
如果(this.revision> version1.Revision)
{
返回1;
}
返回-1;
}
#endregion
///<总结>
/// Equalss指定的obj。
///< /总结>
///< PARAM NAME =目标文件方式>&的OBJ LT; /参数>
///<&回报GT;< /回报>
公众覆盖布尔等于(obj对象)
{
如果((OBJ == NULL)||(obj是的ModuleVersion)!)
{
返回FALSE;
}
的ModuleVersion VERSION1 =(的ModuleVersion)目标文件;
如果(((this.major == version1.Major)及及(this.minor == version1.Minor))及及(this.build == version1.Build)及&放大器; (this.revision == version1.Revision))
{
返回真;
}
返回FALSE;
}
///<总结>
///获取的哈希码。
///< /总结>
///<&回报GT;< /回报>
公共覆盖INT的GetHashCode()
{
INT NUM1 = 0;
NUM1 | =((this.major及15条)LT;<为0x1C);
NUM1 | =((this.minor&放大器; 0xff的)下;&小于20);
NUM1 | =((this.build&放大器; 0xff的)下;&下; 12);
回报率(NUM1 | this.revision&安培; 0xFFF的);
}
///<总结>
/// ==操作符S中指定的V1。
///< /总结>
///&下; PARAM NAME =V1方式> V1&下; /参数>
///< PARAM NAME =V2方式>&V2 LT; /参数>
///<&回报GT;< /回报>
公共静态布尔运算符==(V1的ModuleVersion,V2的ModuleVersion)
{
返回v1.Equals(V2);
}
///<总结>
///操作员及放大器; GT; S指定的V1。
///< /总结>
///&下; PARAM NAME =V1方式> V1&下; /参数>
///< PARAM NAME =V2方式>&V2 LT; /参数>
///<&回报GT;< /回报>
公共静态布尔运算符>(V1的ModuleVersion,V2的ModuleVersion)
{
回报率(V2< V1);
}
///<总结>
///操作员及放大器; GT; = S指定的V1。
///< /总结>
///&下; PARAM NAME =V1方式> V1&下; /参数>
///< PARAM NAME =V2方式>&V2 LT; /参数>
///<&回报GT;< /回报>
公共静态布尔运算符> =(V1的ModuleVersion,V2的ModuleVersion)
{
回报率(V2< = V1);
}
///<总结>
///运算符!= S指定的V1。
///< /总结>
///&下; PARAM NAME =V1方式> V1&下; /参数>
///< PARAM NAME =V2方式>&V2 LT; /参数>
///<&回报GT;< /回报>
公共静态布尔运算符=(V1的ModuleVersion,V2的ModuleVersion)
{
的回报!(V1 = V2!);
}
///<总结>
///操作员及放大器; LT; S指定的V1。
///< /总结>
///&下; PARAM NAME =V1方式> V1&下; /参数>
///< PARAM NAME =V2方式>&V2 LT; /参数>
///<&回报GT;< /回报>
公共静态布尔运算符≤(的ModuleVersion V1,的ModuleVersion V2)
{
如果(V1 == NULL)
{
抛出新的ArgumentNullException(V1) ;
}
返回(v1.CompareTo(V2)小于0);
}
///<总结>
///操作员及放大器; LT; = S指定的V1。
///< /总结>
///&下; PARAM NAME =V1方式> V1&下; /参数>
///< PARAM NAME =V2方式>&V2 LT; /参数>
///<&回报GT;< /回报>
公共静态布尔运算符< =(的ModuleVersion V1,的ModuleVersion V2)
{
如果(V1 == NULL)
{
抛出新的ArgumentNullException(V1 );
}
回报率(v1.CompareTo(V2)< = 0);
}
///<总结>
///脚趾的字符串。
///< /总结>
///<&回报GT;< /回报>
公共重写字符串的ToString()
{
如果(this.build == -1)
{
返回this.ToString(2);
}
如果(this.revision == -1)
{
返回this.ToString(3);
}
返回this.ToString(4);
}
///<总结>
///脚趾的字符串。
///< /总结>
///< PARAM NAME =字段计数方式>字段计数与LT; /参数>
///<&回报GT;< /回报>
公共字符串的ToString(INT字段计数)
{
[对象] objArray1;
开关(字段计数)
{
的情况下0:
{
返回的String.Empty;
}
壳体1:
{
返回(this.major.ToString());
}
壳2:
{
返回(。this.major.ToString()+ this.minor.ToString());
}
}
如果(this.build == -1)
{
抛出新的ArgumentException(的String.Format(ArgumentOutOfRange_Bounds_Lower_Upper {0},{1} ,0,2),字段计数);
}
如果(字段计数== 3)
{
objArray1 =新对象[5] {this.major,。,this.minor,这种。 .build};
返回string.Concat(objArray1);
}
如果(this.revision == -1)
{
抛出新的ArgumentException(的String.Format(ArgumentOutOfRange_Bounds_Lower_Upper {0},{1},0 ,3),字段计数);
}
如果(字段计数== 4)
{
objArray1 =新对象[7] {this.major,。,this.minor,这种。 .build,this.revision}。
返回string.Concat(objArray1);
}
抛出新的ArgumentException(的String.Format(ArgumentOutOfRange_Bounds_Lower_Upper {0},{1},0,4),字段计数);
}
}
}


I've got a class with a System.Version property, which looks like this:

  • Version
    • Build: 111
    • Major: 1
    • MajorRevision: 0
    • Minor: 1
    • MinorRevision: 10
    • Revision: 10

When I serialize the class, version is always empty:

<Version />

The Client class looks like:

[Serializable]
public class Client
{
    public string Description;
    public string Directory;
    public DateTime ReleaseDate;
    public Version Version;
}

解决方案

System.Version is not serializable, if you look at it's properties on MSDN, you'll see they have no setters...so the serializer won't store them. However, this approach still works. That article (old but still works) provides a Version class that is serializable, can you switch to that and get going?

Edit by tomfanning
I have fished the code from the dead site out of archive.org, reproduced below.

using System;
using System.Globalization;
namespace CubicOrange.Version
{
    /// <summary>
    /// Serializable version of the System.Version class.
    /// </summary>
    [Serializable]
    public class ModuleVersion : ICloneable, IComparable
    {
        private int major;
        private int minor;
        private int build;
        private int revision;
        /// <summary>
        /// Gets the major.
        /// </summary>
        /// <value></value>
        public int Major
        {
            get
            {
                return major;
            }
            set
            {
                major = value;
            }
        }
        /// <summary>
        /// Gets the minor.
        /// </summary>
        /// <value></value>
        public int Minor
        {
            get
            {
                return minor;
            }
            set
            {
                minor = value;
            }
        }
        /// <summary>
        /// Gets the build.
        /// </summary>
        /// <value></value>
        public int Build
        {
            get
            {
                return build;
            }
            set
            {
                build = value;
            }
        }
        /// <summary>
        /// Gets the revision.
        /// </summary>
        /// <value></value>
        public int Revision
        {
            get
            {
                return revision;
            }
            set
            {
                revision = value;
            }
        }
        /// <summary>
        /// Creates a new <see cref="ModuleVersion"/> instance.
        /// </summary>
        public ModuleVersion()
        {
            this.build = -1;
            this.revision = -1;
            this.major = 0;
            this.minor = 0;
        }
        /// <summary>
        /// Creates a new <see cref="ModuleVersion"/> instance.
        /// </summary>
        /// <param name="version">Version.</param>
        public ModuleVersion(string version)
        {
            this.build = -1;
            this.revision = -1;
            if (version == null)
            {
                throw new ArgumentNullException("version");
            }
            char[] chArray1 = new char[1] { '.' };
            string[] textArray1 = version.Split(chArray1);
            int num1 = textArray1.Length;
            if ((num1 < 2) || (num1 > 4))
            {
                throw new ArgumentException("Arg_VersionString");
            }
            this.major = int.Parse(textArray1[0], CultureInfo.InvariantCulture);
            if (this.major < 0)
            {
                throw new ArgumentOutOfRangeException("version", "ArgumentOutOfRange_Version");
            }
            this.minor = int.Parse(textArray1[1], CultureInfo.InvariantCulture);
            if (this.minor < 0)
            {
                throw new ArgumentOutOfRangeException("version", "ArgumentOutOfRange_Version");
            }
            num1 -= 2;
            if (num1 > 0)
            {
                this.build = int.Parse(textArray1[2], CultureInfo.InvariantCulture);
                if (this.build < 0)
                {
                    throw new ArgumentOutOfRangeException("build", "ArgumentOutOfRange_Version");
                }
                num1--;
                if (num1 > 0)
                {
                    this.revision = int.Parse(textArray1[3], CultureInfo.InvariantCulture);
                    if (this.revision < 0)
                    {
                        throw new ArgumentOutOfRangeException("revision", "ArgumentOutOfRange_Version");
                    }
                }
            }
        }
        /// <summary>
        /// Creates a new <see cref="ModuleVersion"/> instance.
        /// </summary>
        /// <param name="major">Major.</param>
        /// <param name="minor">Minor.</param>
        public ModuleVersion(int major, int minor)
        {
            this.build = -1;
            this.revision = -1;
            if (major < 0)
            {
                throw new ArgumentOutOfRangeException("major", "ArgumentOutOfRange_Version");
            }
            if (minor < 0)
            {
                throw new ArgumentOutOfRangeException("minor", "ArgumentOutOfRange_Version");
            }
            this.major = major;
            this.minor = minor;
            this.major = major;
        }
        /// <summary>
        /// Creates a new <see cref="ModuleVersion"/> instance.
        /// </summary>
        /// <param name="major">Major.</param>
        /// <param name="minor">Minor.</param>
        /// <param name="build">Build.</param>
        public ModuleVersion(int major, int minor, int build)
        {
            this.build = -1;
            this.revision = -1;
            if (major < 0)
            {
                throw new ArgumentOutOfRangeException("major", "ArgumentOutOfRange_Version");
            }
            if (minor < 0)
            {
                throw new ArgumentOutOfRangeException("minor", "ArgumentOutOfRange_Version");
            }
            if (build < 0)
            {
                throw new ArgumentOutOfRangeException("build", "ArgumentOutOfRange_Version");
            }
            this.major = major;
            this.minor = minor;
            this.build = build;
        }
        /// <summary>
        /// Creates a new <see cref="ModuleVersion"/> instance.
        /// </summary>
        /// <param name="major">Major.</param>
        /// <param name="minor">Minor.</param>
        /// <param name="build">Build.</param>
        /// <param name="revision">Revision.</param>
        public ModuleVersion(int major, int minor, int build, int revision)
        {
            this.build = -1;
            this.revision = -1;
            if (major < 0)
            {
                throw new ArgumentOutOfRangeException("major", "ArgumentOutOfRange_Version");
            }
            if (minor < 0)
            {
                throw new ArgumentOutOfRangeException("minor", "ArgumentOutOfRange_Version");
            }
            if (build < 0)
            {
                throw new ArgumentOutOfRangeException("build", "ArgumentOutOfRange_Version");
            }
            if (revision < 0)
            {
                throw new ArgumentOutOfRangeException("revision", "ArgumentOutOfRange_Version");
            }
            this.major = major;
            this.minor = minor;
            this.build = build;
            this.revision = revision;
        }
        #region ICloneable Members
        /// <summary>
        /// Clones this instance.
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            ModuleVersion version1 = new ModuleVersion();
            version1.major = this.major;
            version1.minor = this.minor;
            version1.build = this.build;
            version1.revision = this.revision;
            return version1;
        }
        #endregion
        #region IComparable Members
        /// <summary>
        /// Compares to.
        /// </summary>
        /// <param name="obj">Obj.</param>
        /// <returns></returns>
        public int CompareTo(object version)
        {
            if (version == null)
            {
                return 1;
            }
            if (!(version is ModuleVersion))
            {
                throw new ArgumentException("Arg_MustBeVersion");
            }
            ModuleVersion version1 = (ModuleVersion)version;
            if (this.major != version1.Major)
            {
                if (this.major > version1.Major)
                {
                    return 1;
                }
                return -1;
            }
            if (this.minor != version1.Minor)
            {
                if (this.minor > version1.Minor)
                {
                    return 1;
                }
                return -1;
            }
            if (this.build != version1.Build)
            {
                if (this.build > version1.Build)
                {
                    return 1;
                }
                return -1;
            }
            if (this.revision == version1.Revision)
            {
                return 0;
            }
            if (this.revision > version1.Revision)
            {
                return 1;
            }
            return -1;
        }
        #endregion
        /// <summary>
        /// Equalss the specified obj.
        /// </summary>
        /// <param name="obj">Obj.</param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if ((obj == null) || !(obj is ModuleVersion))
            {
                return false;
            }
            ModuleVersion version1 = (ModuleVersion)obj;
            if (((this.major == version1.Major) && (this.minor == version1.Minor)) && (this.build == version1.Build) && (this.revision == version1.Revision))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// Gets the hash code.
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            int num1 = 0;
            num1 |= ((this.major & 15) << 0x1c);
            num1 |= ((this.minor & 0xff) << 20);
            num1 |= ((this.build & 0xff) << 12);
            return (num1 | this.revision & 0xfff);
        }
        /// <summary>
        /// Operator ==s the specified v1.
        /// </summary>
        /// <param name="v1">V1.</param>
        /// <param name="v2">V2.</param>
        /// <returns></returns>
        public static bool operator ==(ModuleVersion v1, ModuleVersion v2)
        {
            return v1.Equals(v2);
        }
        /// <summary>
        /// Operator &gt;s the specified v1.
        /// </summary>
        /// <param name="v1">V1.</param>
        /// <param name="v2">V2.</param>
        /// <returns></returns>
        public static bool operator >(ModuleVersion v1, ModuleVersion v2)
        {
            return (v2 < v1);
        }
        /// <summary>
        /// Operator &gt;=s the specified v1.
        /// </summary>
        /// <param name="v1">V1.</param>
        /// <param name="v2">V2.</param>
        /// <returns></returns>
        public static bool operator >=(ModuleVersion v1, ModuleVersion v2)
        {
            return (v2 <= v1);
        }
        /// <summary>
        /// Operator !=s the specified v1.
        /// </summary>
        /// <param name="v1">V1.</param>
        /// <param name="v2">V2.</param>
        /// <returns></returns>
        public static bool operator !=(ModuleVersion v1, ModuleVersion v2)
        {
            return (v1 != v2);
        }
        /// <summary>
        /// Operator &lt;s the specified v1.
        /// </summary>
        /// <param name="v1">V1.</param>
        /// <param name="v2">V2.</param>
        /// <returns></returns>
        public static bool operator <(ModuleVersion v1, ModuleVersion v2)
        {
            if (v1 == null)
            {
                throw new ArgumentNullException("v1");
            }
            return (v1.CompareTo(v2) < 0);
        }
        /// <summary>
        /// Operator &lt;=s the specified v1.
        /// </summary>
        /// <param name="v1">V1.</param>
        /// <param name="v2">V2.</param>
        /// <returns></returns>
        public static bool operator <=(ModuleVersion v1, ModuleVersion v2)
        {
            if (v1 == null)
            {
                throw new ArgumentNullException("v1");
            }
            return (v1.CompareTo(v2) <= 0);
        }
        /// <summary>
        /// Toes the string.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (this.build == -1)
            {
                return this.ToString(2);
            }
            if (this.revision == -1)
            {
                return this.ToString(3);
            }
            return this.ToString(4);
        }
        /// <summary>
        /// Toes the string.
        /// </summary>
        /// <param name="fieldCount">Field count.</param>
        /// <returns></returns>
        public string ToString(int fieldCount)
        {
            object[] objArray1;
            switch (fieldCount)
            {
                case 0:
                    {
                        return string.Empty;
                    }
                case 1:
                    {
                        return (this.major.ToString());
                    }
                case 2:
                    {
                        return (this.major.ToString() + "." + this.minor.ToString());
                    }
            }
            if (this.build == -1)
            {
                throw new ArgumentException(string.Format("ArgumentOutOfRange_Bounds_Lower_Upper {0},{1}", "0", "2"), "fieldCount");
            }
            if (fieldCount == 3)
            {
                objArray1 = new object[5] { this.major, ".", this.minor, ".", this.build };
                return string.Concat(objArray1);
            }
            if (this.revision == -1)
            {
                throw new ArgumentException(string.Format("ArgumentOutOfRange_Bounds_Lower_Upper {0},{1}", "0", "3"), "fieldCount");
            }
            if (fieldCount == 4)
            {
                objArray1 = new object[7] { this.major, ".", this.minor, ".", this.build, ".", this.revision };
                return string.Concat(objArray1);
            }
            throw new ArgumentException(string.Format("ArgumentOutOfRange_Bounds_Lower_Upper {0},{1}", "0", "4"), "fieldCount");
        }
    }
}

这篇关于System.Version没有序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆