包含属性的Enum-Like对象

时间:2011-10-30 19:14:01

标签: c#-4.0 enums properties switch-statement

我试图想办法让一个类充满静态对象,每个静态对象都有各种静态属性。

我希望能够传递这些属性,甚至将它们设置为其他对象的静态属性,我也希望能够切换对象。

这是一个说明我的意思的例子:

创建和发送消息

class Program
{
    static void Main(string[] args)
    {
        MarketOrder Order = new MarketOrder("DELL", MessageProperties.SecurityType.Equity, MessageProperties.ExchangeDestination.ARCA.PostOnly);

        SendOrder(Order);

        Console.ReadLine();
    }


    public static void SendOrder(MarketOrder Order)
    {
        switch (Order.SecurityType)
        {
            case MessageProperties.SecurityType.Equity:
                // Equity sending logic here
                break;

            case MessageProperties.SecurityType.Option:
                // Option sending logic here
                break;

            case MessageProperties.SecurityType.Future:
                // Future sending logic here
                break;
        }
    }
}

这不想编译,因为它不会让我切换Order.SecurityType对象。

MarketOrder Class

public class MarketOrder
{
    public readonly string Symbol;
    public readonly MessageProperties.SecurityType SecurityType;
    public readonly MessageProperties.ExchangeDestination ExchangeDestination;

    public MarketOrder(string Symbol, MessageProperties.SecurityType SecurityType, MessageProperties.ExchangeDestination ExchangeDestination)
    {
        this.Symbol                 = Symbol;
        this.SecurityType           = SecurityType;
        this.ExchangeDestination    = ExchangeDestination;
    }
}

MessageProperties Class

public abstract class MessageProperties
{
    public class ExchangeDestination
    {
        public readonly string  Value;
        public readonly double  ExchangeFee;
        public ExchangeDestination(string Value, double ExchangeFeed)
        {
            this.Value = Value;
            this.ExchangeFee = ExchangeFee;
        }

        public abstract class ARCA
        {
            public static ExchangeDestination Only      = new ExchangeDestination("ARCA.ONLY", 0.01);
            public static ExchangeDestination PostOnly  = new ExchangeDestination("ARCA.ONLYP", 0.02);
        }

        public abstract class NYSE
        {
            public static ExchangeDestination Only      = new ExchangeDestination("NYSE.ONLY", 0.01);
            public static ExchangeDestination PostOnly  = new ExchangeDestination("NYSE.ONLYP", 0.03);
        }
    }


    public class SecurityType
    {
        public readonly string Value;
        public SecurityType(string Value)
        {
            this.Value = Value;
        }

        public static SecurityType Equity = new SecurityType("EQ");
        public static SecurityType Option = new SecurityType("OPT");
        public static SecurityType Future = new SecurityType("FUT");
    }
}

Enums可以完美地完成我想要做的事情,除了很难拥有枚举值的多个属性。我考虑使用枚举上的属性来设置属性,但是获取这些属性与获取对象的静态属性相比要慢得多,而且我的应用程序对速度/延迟非常敏感。

是否有更好的方法来完成我想要做的事情?

提前感谢您的帮助!

威廉

0 个答案:

没有答案