Laden...

Statische Operatoren in generischer Klasse

Erstellt von HeikoAdams vor 5 Jahren Letzter Beitrag vor 5 Jahren 996 Views
HeikoAdams Themenstarter:in
62 Beiträge seit 2017
vor 5 Jahren
Statische Operatoren in generischer Klasse

Hallo,
ich habe eine generische Klasse (im Anhang), die statice Operatoren implementiert und weshalb mich FxCop mit dem CA1000 anmeckert.

namespace SKTools.Types
{
    using System;
    using System.Text;

    /// <summary>
    /// A Intervall-implementation for different Types.
    /// </summary>
    /// <typeparam name="T">Type of the Rang. Must be comparable.</typeparam>
    public class Range<T> where T : struct, IComparable<T>
    {

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="d">The value.</param>
        /// <param name="r">The range.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(T d, Range<T> r) => !r.Contains(d);

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="r">The range.</param>
        /// <param name="d">The value.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(Range<T> r, T d) => !r.Contains(d);

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="r1">The first range.</param>
        /// <param name="r2">The second range</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(Range<T> r1, Range<T> r2) => !r1.Equals(r2);

        /// <summary>
        /// Implements the operator &lt;.
        /// </summary>
        /// <param name="d">The value.</param>
        /// <param name="r">The range.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator <(T d, Range<T> r)
        {
            if (d.CompareTo(r.LeftValue) < 0)
            {
                return true;
            }
            else if (d.CompareTo(r.LeftValue) == 0 && !r.LeftClosed)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Implements the operator &lt;.
        /// </summary>
        /// <param name="r">The range.</param>
        /// <param name="d">The value.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator <(Range<T> r, T d)
        {
            if (r.RightValue.CompareTo(d) < 0)
            {
                return true;
            }
            else if (r.RightValue.CompareTo(d) == 0 && !r.RightClosed)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Implements the operator &lt;=.
        /// </summary>
        /// <param name="d">The value.</param>
        /// <param name="r">The range.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator <=(T d, Range<T> r)
        {
            if (d.CompareTo(r.RightValue) == 0 && r.RightClosed)
            {
                return true;
            }
            else if (d.CompareTo(r.RightValue) < 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Implements the operator &lt;=.
        /// </summary>
        /// <param name="r">The range.</param>
        /// <param name="d">The value.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator <=(Range<T> r, T d)
        {
            if (r.LeftValue.CompareTo(d) == 0 && r.LeftClosed)
            {
                return true;
            }
            else if (r.LeftValue.CompareTo(d) < 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="d">The value.</param>
        /// <param name="r">The range.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(T d, Range<T> r) => r.Contains(d);

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="r">The range.</param>
        /// <param name="d">The value.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(Range<T> r, T d) => r.Contains(d);

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="r1">The first range.</param>
        /// <param name="r2">The second range</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(Range<T> r1, Range<T> r2) => r1.Equals(r2);

        /// <summary>
        /// Implements the operator &gt;.
        /// </summary>
        /// <param name="d">The value.</param>
        /// <param name="r">The range.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator >(T d, Range<T> r)
        {
            if (d.CompareTo(r.RightValue) > 0)
            {
                return true;
            }
            else if (d.CompareTo(r.RightValue) == 0 && !r.RightClosed)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Implements the operator &gt;.
        /// </summary>
        /// <param name="r">The range.</param>
        /// <param name="d">The value.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator >(Range<T> r, T d)
        {
            if (r.LeftValue.CompareTo(d) > 0)
            {
                return true;
            }
            else if (r.LeftValue.CompareTo(d) == 0 && !r.LeftClosed)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Implements the operator &gt;=.
        /// </summary>
        /// <param name="d">The value.</param>
        /// <param name="r">The range.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator >=(T d, Range<T> r)
        {
            if (d.CompareTo(r.LeftValue) == 0 && r.LeftClosed)
            {
                return true;
            }
            else if (d.CompareTo(r.LeftValue) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Implements the operator &gt;=.
        /// </summary>
        /// <param name="r">The range.</param>
        /// <param name="d">The value.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator >=(Range<T> r, T d)
        {
            if (r.RightValue.CompareTo(d) == 0 && r.RightClosed)
            {
                return true;
            }
            else if (r.RightValue.CompareTo(d) > 0)
            {
                return true;
            }
            return false;
        }
    }
}

Irgendeine Idee, wie man die Warnung los wird, ohne sie zu unterdrücken?

Hinweis von Coffeebean vor 5 Jahren

Habe die Datei als Anhang mal rausgeschmissen. Bitte nur relevante Codeausschnitte posten. Das hilft uns dir zu helfen. [Hinweis] Wie poste ich richtig?

Wer ordentlichen Code schreibt, lebt entspannter 8)

W
955 Beiträge seit 2010
vor 5 Jahren

Hi,
wenn du Operatoren überladen willst sollten die Signaturen stimmen. Sowas wie

public static bool operator !=(T d, Range<T> r) => !r.Contains(d);

ist sehr ungewöhnlich (und auch schwer verständlich, Äpfel mit Äpelbäumen zu vergleichen). Du kannst mal prüfen ob die Warnung verschwindet wenn du diese durch gewöhnliche Methoden ersetzt.

1.029 Beiträge seit 2010
vor 5 Jahren

Hi,

verschwinden wird die Warnung nicht soweit ich es verstehe.

Im Prinzip steht ja selbst in der MSDN, dass ein generischer Typ keine statischen Member beinhalten soll(te) -> daher auch die Warnung.

Da die Möglichkeit Operatoren in Instanzmember zu verwandeln nicht besteht - blendest du die Warnung entweder aus oder lebst damit.

Begründung von Microsoft lautet ja, dass Bibliotheken so "leichter angenommen" werden...

LG