Question

I had to build my own Version class. In .NET it's CLS Compliant but in Mono its not for some reason. Any ideas why?

[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);
    }
}

The custom exception classes I throw are simply wrappers around ArgumentNullException that "auto fill" the message for me so they aren't adding any types to it really.

No correct solution

OTHER TIPS

If it's CLS compliant in .NET but not on Mono, it is a bug in Mono. Please file a bug in http://bugzilla.xamarin.com

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top