EnumParentAttribute and Intellisense

I have a class with an enum

namespace Entities
{
    public partial class OrderStatusType : Entity
    {
        public enum EOrderStatusType
        {
            P2 = 1,
            Mounting = 2,
        }
    }
}

then I have an interface

namespace Interfaces
{
    public interface IAttribute<T>
    {
        T Value { get; }
    }

}

and another class

namespace Entities
{
    public partial class OrderStatus : Entity
    {
        [DataContract]
        public enum EOrderStatus
        {
            [EnumMember(Value = "Film Ordered"), EnumParent(OrderStatusType.EOrderStatusType.P2)]
            FilmOrdered = 1,
            [EnumMember(Value = "Not Started"), EnumParent(OrderStatusType.EOrderStatusType.P2)]
            NotStarted = 2,
            [EnumMember(Value = "Cliche Ordered"), EnumParent(OrderStatusType.EOrderStatusType.P2)]
            ClicheOrdered = 3,
            [EnumMember(Value = "Cliche In Stock"), EnumParent(OrderStatusType.EOrderStatusType.P2)]
            ClicheInStock = 4,
            [EnumMember(Value = "Cliche Prepared"), EnumParent(OrderStatusType.EOrderStatusType.P2)]
            ClichePrepared = 5,
            [EnumMember(Value = "Ready to start"), EnumParent(OrderStatusType.EOrderStatusType.P2)]
            Readytostart = 6,
            [EnumMember(Value = "Started"), EnumParent(OrderStatusType.EOrderStatusType.P2)]
            Started = 7,
            [EnumMember(Value = "Interrupted"), EnumParent(OrderStatusType.EOrderStatusType.P2)]
            Interrupted = 8,
            [EnumMember(Value = "Completed"), EnumParent(OrderStatusType.EOrderStatusType.P2)]
            Completed = 9,
            [EnumMember(Value = "ClichV status"), EnumParent(OrderStatusType.EOrderStatusType.P2)]
            ClichVstatus = 10,
            [EnumMember(Value = "Cliche Mounting"), EnumParent(OrderStatusType.EOrderStatusType.P2)]
            ClicheMounting = 11,
            [EnumMember(Value = "In Stock"), EnumParent(OrderStatusType.EOrderStatusType.P2)]
            InStock = 12,
            [EnumMember(Value = "Ordine con cilindri montati"), EnumParent(OrderStatusType.EOrderStatusType.Mounting)]
            OrdineConCilindriMontati = 13,
        }

        [AttributeUsage(AttributeTargets.Field)]
        public sealed class EnumParentAttribute : Attribute, Library.Interfaces.IAttribute<OrderStatusType.EOrderStatusType>
        {
            public EnumParentAttribute(OrderStatusType.EOrderStatusType EOrderStatusType) { this.mValue = EOrderStatusType; }

            private OrderStatusType.EOrderStatusType mValue;

            public OrderStatusType.EOrderStatusType Value { get { return mValue; } }
        }
    }
}

In class OrderStatus I create a sealed subclass EnumParentAttribute that inherits from attribute and implements the interface and where I can pass a parameter of type OrderStatusType.EOrderStatusType, so now I can decorate the items of the enum EOrderStatus with the custom attribute EnumParent, that accept a parameter of type OrderStatusType.EOrderStatusType.

The reason of that is that I want to create a sort of son-parent realtionship between two enums. But I'd like to take another step or two...

Question:

1) How can I add a "constarint" that forces me to decorate a new EOrderStatusType item with the custom attribute EnumParent? There is a way to ask to compiler to notify an exception during compile? 2) How can I extend the EOrderStatusType enum so that I can write a code like this: OrderStatus.EOrderStatusType(P2) l_MyEnum = OrderStatus.EOrderStatus.FilmOrdered; where the Intellisense shows me only the items with the "right" parent?

Sorry for my english, I hope it is quite understandable...

Thanks in advance!

Jon Skeet
people
quotationmark

How can I add a "constarint" that forces me to decorate a new EOrderStatusType item with the custom attribute EnumParent? There is a way to ask to compiler to notify an exception during compile?

Two options:

  • Add a unit test. This is dead easy to do, and it'll hopefully stop you from submitting bad code (assuming you don't submit without running the tests) but it won't actually show up at compile time.
  • Use Visual Studio 2015 and Roslyn to hook into the compiler, and create a code diagnostic that shows this up as an error. (You'd probably want to make it depend on an attribute declared on the enum itself, rather than hard-coding it to one particular enum.) Of course, Visual Studio 2015 is only in preview as I write this - but it will become an increasingly reasonable solution over time.

How can I extend the EOrderStatusType enum so that I can write a code like this: OrderStatus.EOrderStatusType(P2) l_MyEnum = OrderStatus.EOrderStatus.FilmOrdered;

I don't think you can do this even with Roslyn, to be honest.

As an aside, I'd strongly recommend that you ditch the E prefix from your enum types. Ick :)

people

See more on this question at Stackoverflow