0

我必须建立自己的 Version 类。在 .NET 中它符合 CLS,但在 Mono 中它不是出于某种原因。任何想法为什么?

[Serializable]
public class Version : ICloneable, IComparable, IComparable<Version>, IEquatable<Version>
{
    private int major;
    private int minor;
    private int revision;
    private int build;

    protected Version()
    {
    }

    public Version(int major, int minor)
    {
        Major = major;
        Minor = minor;
    }

    public Version(int major, int minor, int revision, int build) : this(major, minor)
    {
        Revision = revision;
        Build = build;
    }

    public Version(string version)
    {
        if (string.IsNullOrWhiteSpace(version))
        {
            throw new ControlInfluence.Exceptions.ArgumentNullStringException("version");
        }

        string[] parts = version.Split('.');
        if ((parts.Length != 4) && (parts.Length != 2))
        {
            throw new ArgumentException("'version' must have 2 or 4 numbers separated by '.'.", "version");
        }
        if (!int.TryParse(parts[0], out major))
        {
            throw new ArgumentException("'version' must have 2 or 4 numbers separated by '.'.", "version");
        }
        if (!int.TryParse(parts[1], out minor))
        {
            throw new ArgumentException("'version' must have 2 or 4 numbers separated by '.'.", "version");
        }
        if (!int.TryParse(parts[2], out revision))
        {
            throw new ArgumentException("'version' must have 2 or 4 numbers separated by '.'.", "version");
        }
        if (!int.TryParse(parts[3], out build))
        {
            throw new ArgumentException("'version' must have 2 or 4 numbers separated by '.'.", "version");
        }
    }

    public int Major
    {
        get
        {
            return major;
        }
        set
        {
            major = value;
        }
    }

    public int Minor
    {
        get
        {
            return minor;
        }
        set
        {
            minor = value;
        }
    }

    public int Build
    {
        get
        {
            return build;
        }
        set
        {
            build = value;
        }
    }

    public int Revision
    {
        get
        {
            return revision;
        }
        set
        {
            revision = value;
        }
    }

    public override string ToString()
    {
        return string.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}.{3}", Major, Minor, Revision, Build);
    }

    public static bool operator <(Version left, Version right)
    {
        if ((left == null) || (right == null))
        {
            return false;
        }

        return left.CompareTo(right) < 0;
    }

    public static bool operator >(Version left, Version right)
    {
        if ((left == null) || (right == null))
        {
            return false;
        }

        return left.CompareTo(right) > 0;
    }

    #region ICloneable Members

    public object Clone()
    {
        return new Version(Major, Minor, Revision, Build);
    }

    #endregion

    #region IComparable Members

    public int CompareTo(object obj)
    {
        Version other = obj as Version;
        if (other == null)
        {
            return -1;
        }
        return CompareTo(other);
    }

    #endregion

    #region IComparable<Version> Members

    public int CompareTo(Version other)
    {
        if (other == null)
        {
            return -1;
        }

        int compareMajor = Major.CompareTo(other.Major);
        if (compareMajor != 0)
        {
            return compareMajor;
        }
        int compareMinor = Minor.CompareTo(other.Minor);
        if (compareMinor != 0)
        {
            return compareMinor;
        }
        int compareRevision = Revision.CompareTo(other.Revision);
        if (compareRevision != 0)
        {
            return compareRevision;
        }
        return Build.CompareTo(other.Build);
    }

    #endregion

    #region IEquatable<Version> Members

    public bool Equals(Version other)
    {
        return CompareTo(other) == 0;
    }

    #endregion

    public override bool Equals(object obj)
    {
        Version version = obj as Version;
        if (version == null)
        {
            return false;
        }
        return Equals(version);
    }

    public static bool operator ==(Version left, Version right)
    {
        if (Object.ReferenceEquals(left, null) && Object.ReferenceEquals(right, null))
        {
            return true;
        }
        if (Object.ReferenceEquals(left, null) || Object.ReferenceEquals(right, null))
        {
            return false;
        }

        return left.Equals(right);
    }

    public static bool operator !=(Version left, Version right)
    {
        if ((left == null) || (right == null))
        {
            return false;
        }

        return !left.Equals(right);
    }
}

我抛出的自定义异常类只是 ArgumentNullException 的包装器,它为我“自动填充”消息,因此它们实际上并没有添加任何类型。

4

1 回答 1

1

如果它在 .NET 中与 CLS 兼容,但在 Mono 上不兼容,则它是 Mono 中的错误。请在http://bugzilla.xamarin.com中提交错误

于 2013-02-21T10:36:01.883 回答