I\'ve seen a couple similar threads to this question, but none of them really answer the question I want to ask.
For starters, unfortunately I\'m working with existing A
You can go with your approach in a slightly different way:
public interface IError
{
Enum ErrorCode;
string Description;
}
System.Enum
is the base class of all your enums, so that should handle it, but its far from being expressive.
The right approach here is to build your own enum classes and a base enum class for it. For eg.,
public class ErrorFlag // base enum class
{
int value;
ErrorFlag()
{
}
public static implicit operator ErrorFlag(int i)
{
return new ErrorFlag { value = i };
}
public bool Equals(ErrorFlag other)
{
if (ReferenceEquals(this, other))
return true;
if (ReferenceEquals(null, other))
return false;
return value == other.value;
}
public override bool Equals(object obj)
{
return Equals(obj as ErrorFlag);
}
public static bool operator ==(ErrorFlag lhs, ErrorFlag rhs)
{
if (ReferenceEquals(lhs, null))
return ReferenceEquals(rhs, null);
return lhs.Equals(rhs);
}
public static bool operator !=(ErrorFlag lhs, ErrorFlag rhs)
{
return !(lhs == rhs);
}
public override int GetHashCode()
{
return value;
}
public override string ToString()
{
return value.ToString();
}
}
public interface IError
{
ErrorFlag ErrorCode;
string Description;
}
Now instead of having your own error enums, write your own ErrorFlag
classes.
public sealed class ReportErrorFlag : ErrorFlag
{
//basically your enum values
public static readonly ErrorFlag Report1 = 1;
public static readonly ErrorFlag Report2 = 2;
public static readonly ErrorFlag Report3 = 3;
ReportErrorFlag()
{
}
}
public sealed class DataErrorFlag : ErrorFlag
{
//basically your enum values
public static readonly ErrorFlag Data1 = 1;
public static readonly ErrorFlag Data2 = 2;
public static readonly ErrorFlag Data3 = 3;
DataErrorFlag()
{
}
}
// etc
Now your main classes:
public class ReportError : IError
{
// implementation
}
public class DataError : IError
{
// implementation
}
Or otherwise,
public class ErrorFlag // base enum class
{
internal int value { get; set; }
public bool Equals(ErrorFlag other)
{
if (ReferenceEquals(this, other))
return true;
if (ReferenceEquals(null, other))
return false;
return value == other.value;
}
public override bool Equals(object obj)
{
return Equals(obj as ErrorFlag);
}
public static bool operator ==(ErrorFlag lhs, ErrorFlag rhs)
{
if (ReferenceEquals(lhs, null))
return ReferenceEquals(rhs, null);
return lhs.Equals(rhs);
}
public static bool operator !=(ErrorFlag lhs, ErrorFlag rhs)
{
return !(lhs == rhs);
}
public override int GetHashCode()
{
return value;
}
public override string ToString()
{
return value.ToString();
}
}
public interface IError where T : ErrorFlag
{
T ErrorCode { get; set; }
string Description { get; set; }
}
//enum classes
public sealed class ReportErrorFlag : ErrorFlag
{
//basically your enum values
public static readonly ReportErrorFlag Report1 = new ReportErrorFlag { value = 1 };
public static readonly ReportErrorFlag Report2 = new ReportErrorFlag { value = 2 };
public static readonly ReportErrorFlag Report3 = new ReportErrorFlag { value = 3 };
ReportErrorFlag()
{
}
}
public sealed class DataErrorFlag : ErrorFlag
{
//basically your enum values
public static readonly DataErrorFlag Data1 = new DataErrorFlag { value = 1 };
public static readonly DataErrorFlag Data2 = new DataErrorFlag { value = 2 };
public static readonly DataErrorFlag Data3 = new DataErrorFlag { value = 3 };
DataErrorFlag()
{
}
}
//implement the rest
To have ugly way of having enum constraints, see Anyone know a good workaround for the lack of an enum generic constraint?