Domanda

Possible Duplicate:
When are two enums equal in C#?

I have the following classes as part of a simple state machine.

Please note that all generic type parameters HAVE to be an enumeration. That has been enforced in the constructors (not shown here).

// Both [TState] and [TCommand] will ALWAYS be enumerations.
public class Transitions<TState, TCommand>: List<Transition<TState, TCommand>>
{
    public new void Add (Transition<TState, TCommand> item)
    {
        if (this.Contains(item))
            throw (new InvalidOperationException("This transition already exists."));
        else
            this.Add(item);
    }
}

// Both [TState] and [TCommand] will ALWAYS be enumerations.
public class Transition<TState, TCommand>
{
    TState From = default(TState);
    TState To  = default(TState);
    TCommand Command = default(TCommand);
}

public sealed class TransitionComparer<TState>:
    IComparer<TState>
{
    public int Compare (TState x, TState y)
    {
        int result = 0;

        // How to compare here since TState is not strongly typed and is an enum?
        //    Using ToString seems silly here.

        result |= x.From.ToString().CompareTo(y.From.ToString());
        result |= x.To.ToString().CompareTo(y.To.ToString());
        result |= x.Command.ToString().CompareTo(y.Command.ToString());

        return (result);
    }
}

The above does compile but I'm not sure if this is the right way to handle enums that have been passed in as generic type parameters.

Note: The compare function does not need to keep ordering in mind. Rather it needs to check for exact duplicates.

È stato utile?

Soluzione

Note: The compare function does not need to keep ordering in mind. Rather it needs to check for exact duplicates.

In that case, you shouldn't be implementing IComparer<T>. You should be implementing IEqualityComparer<T> - or, more simply, make Transition implement IEquatable<Transition>.

Note that you're currently not using the TransitionComparer in the rest of the code, as far as we can see. It sounds like you shouldn't really need to write your own comparison code for each enum value - you're just trying to combine them. Unfortunately enums don't implement IEquatable<T>, which makes is a bit harder to do this without boxing - how performance critical is this?

Here's a sample equality implementation for Transition:

public class Transition<TState, TCommand>
    : IEquatable<Transition<TState, TCommand>>
{
    // I assume in reality these are properties?
    TState From = default(TState);
    TState To  = default(TState);
    TCommand Command = default(TCommand);

    public override bool Equals(object other)
    {
        return Equals(other as Transition<TState, TCommand>);
    }

    public bool Equals(Transition<TState, TCommand> other)
    {
        if (other == null)
        {
            return false;
        }
        return From.Equals(other.From) &&
               To.Equals(other.To) &&
               Command.Equals(other.Command);
    }

    public int GetHashCode()
    {
        int hash = 17;
        hash = hash * 31 + From.GetHashCode();
        hash = hash * 31 + To.GetHashCode();
        hash = hash * 31 + Command.GetHashCode();
        return hash;
    }
}

EDIT: To avoid boxing for equality, I suspect you would either need some kind of delegate to get at the underlying value (and if you need to support values with different underlying types, that's pretty painful) or potentially use something like Unconstrained Melody which uses IL rewriting to enforce the constraint at compile time, and allows you to check for equality based on underlying value more efficiently.

Altri suggerimenti

I can't see anything than

(int)(object)x.From.CompareTo((int)(object)y.From);

The enums are boxed. I don't know how this could be avoided.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top