I\'m making a server library in which the packet association is done by enum.
public enum ServerOperationCode : byte
{
LoginResponse = 0x00,
Selectio
Why does everyone think that Enums cannot be abstracted?
The class System.Enum IS the abstraction of an enumeration.
You can assign any enumeration value to an Enum, and you can cast it back to the original enumeration or use the name or value.
eg:
This little snippet of code is from a dynamic property collection used in one of my control libraries. I allow properties to be created and accessed through an enumeration value to make it slightly faster, and less human-error
///
/// creates a new trigger property.
///
///
///
///
///
protected virtual TriggerProperty Create(T value, Enum name)
{
var pt = new TriggerProperty(value, OnPropertyChanged, Enum.GetName(name.GetType(), name));
_properties[name.GetHashCode()] = pt;
return pt;
}
I use Enum.GetName(Type, object)
to get the name of the enumeration value (to supply a name for the property), and for speed and consistency reasons I use GetHashCode()
to return the integer value of the enumeration member (the hash code for an int is always just the int value)
This is an example of the method being called:
public enum Props
{
A, B, C, Color, Type, Region, Centre, Angle
}
public SpecularProperties()
:base("SpecularProperties", null)
{
Create(1, Props.A);
Create(1, Props.B);
Create(1, Props.C);
Create(Color.Gray, Props.Color);
Create(GradientType.Linear, Props.Type);
Create(RectangleF.Empty, Props.Region);
Create(PointF.Empty, Props.Centre);
Create(0f, Props.Angle);
}