什么是单个键/值对属性附加到一个枚举的最佳方法? [英] What is the best method for attaching a single key/value pair attribute to an enumeration?

查看:154
本文介绍了什么是单个键/值对属性附加到一个枚举的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出最好的方法是什么一个键/值对属性附加到一个枚举,其中最关键的是我的 MERCHANTID 和值对应的 TransactionKey

I'm trying to figure out what the best method is for attaching a single key/value pair attribute to an enumeration where the key is my MerchantId and the value is the corresponding TransactionKey.

我目前做的是把一个逗号分隔字符串转换为 StringValueAttribute 类:

What I currently do is put a comma delimited string into a StringValueAttribute class:

Public Enum Merchants
    <StringValue("coke,faj80785hq+faf=-1=-jfa+">
        Coke = 0
    <StringValue("pepsi,adfji=-901jnas++fdj98ua")>
        Pepsi = 1
    <StringValue("drpepper,jk878-=+9kdkdja0=a=f--daj")>
        DrPepper = 2
End Enum

Public Property Merchant As Merchants



我拔出钥匙或 MERCHANTID 致电 .GetStringValue()斯普利特(,C)(0)

Public ReadOnly Property MerchantId() As String
    Get
        Return Merchant.GetStringValue().Split(","c)(0)
    End Get
End Property



我通过调用 .GetStringValue()拉出值或 TransactionKey 斯普利特(,C)(1)


I pull out the value or TransactionKey by calling .GetStringValue().Split(","c)(1):

Public ReadOnly Property TransactionKey() As String
    Get
        Return Merchant.GetStringValue().Split(","c)(1)
    End Get
End Property


这是最有效的方式做到这一点?取而代之的 StringValueAttribute ,怎么样使用词典(字符串,字符串)创建一个属性,因为它是一个关键的/值对列表?或字符串数​​组或列表?也许东西在LINQ?或者是干脆已经有效的,因为它可以?


Is this the most efficient way to do this? Instead of StringValueAttribute, what about creating an attribute using a Dictionary(Of String, String) since it is a key/value pair list? Or String Array or List? Maybe something in LINQ? Or is it simply already efficient as it can be?

推荐答案

您可以使用下面的自定义属性和扩展方法得到的值。需要注意以下几点:

You can use the following custom attribute and extension methods to get the values. Some things to note:


  • 这是在C#中,希望这是确定:)

  • It's in C#, hope that's ok :)

的扩展方法类缓存静态范围MerchantIds和TransactionIds,所以应该是pretty效率。

The extension methods class caches the MerchantIds and TransactionIds in static scope, so should be pretty efficient.

您获得 MERCHANTID 通过调用(例如的) Merchants.Coke.GetMerchantId();

You get the MerchantId by calling (e.g.) Merchants.Coke.GetMerchantId();.

您获得 TRANSACTIONID 通过调用(例如的) Merchants.Coke.GetTransactionId();

You get the TransactionId by calling (e.g.) Merchants.Coke.GetTransactionId();.

此外,扩展方法也懒得检查传递给它们的招商局值是有效的,所以你可以通过调用打破它( (招商)76282).GetMerchantId()

Also, the extension methods don't bother checking that the Merchants value passed to them is valid, so you could break it by calling ((Merchants)76282).GetMerchantId().

[AttributeUsage(AttributeTargets.Field)]
public class MerchantDataAttribute : Attribute
{
    public MerchantDataAttribute(string merchantId, string transactionId)
    {
        this.MerchantId = merchantId;
        this.TransactionId = transactionId;
    }

    public string MerchantId
    {
        get;
        private set;
    }

    public string TransactionId
    {
        get;
        private set;
    }
}

public static class MerchantsExtensions
{
    private static readonly Dictionary<Merchants, MerchantDataAttribute> 
        _merchantsCache = CacheMerchantsCache();

    public static string GetMerchantId(this Merchants merchants)
    {
        return _merchantsCache[merchants].MerchantId;
    }

    public static string GetTransactionId(this Merchants merchants)
    {
        return _merchantsCache[merchants].TransactionId;
    }

    private static Dictionary<Merchants, MerchantDataAttribute> CacheMerchantsCache()
    {
        return Enum.GetValues(typeof(Merchants))
            .Cast<Merchants>()
            .Select(m => new
            {
                Merchant = m, 
                MerchantAttribute = GetMerchantAttribute(m)
            })
            .ToDictionary(m => m.Merchant, m => m.MerchantAttribute);
    }

    private static MerchantDataAttribute GetMerchantAttribute(Merchants merchant)
    {
        return typeof(Merchants)
            .GetMember(merchant.ToString())
            .First()
            .GetCustomAttributes(typeof(MerchantDataAttribute), inherit: false)
            .Cast<MerchantDataAttribute>()
            .First();
    }
}

这篇关于什么是单个键/值对属性附加到一个枚举的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆