Asp.net 将单个键/值对属性附加到枚举的最佳方法是什么?
我试图找出将单个键/值对属性附加到枚举的最佳方法,其中键是myMerchantId,值是对应的TransactionKey 我当前要做的是将逗号分隔的字符串放入Asp.net 将单个键/值对属性附加到枚举的最佳方法是什么?,asp.net,vb.net,dictionary,enums,Asp.net,Vb.net,Dictionary,Enums,我试图找出将单个键/值对属性附加到枚举的最佳方法,其中键是myMerchantId,值是对应的TransactionKey 我当前要做的是将逗号分隔的字符串放入StringValueAttribute类中: 公共枚举商户 焦炭=0 百事可乐=1 DrPepper=2 结束枚举 作为商人的公共财产 我通过调用.GetStringValue().Split(“,”c)(0),拉出键或MerchantId: 公共只读属性MerchantId()作为字符串 得到 返回Merchant.GetStri
StringValueAttribute
类中:
公共枚举商户
焦炭=0
百事可乐=1
DrPepper=2
结束枚举
作为商人的公共财产
我通过调用
.GetStringValue().Split(“,”c)(0)
,拉出键或MerchantId:
公共只读属性MerchantId()作为字符串
得到
返回Merchant.GetStringValue().Split(“,”c)(0)
结束
端属性
我通过调用
.GetStringValue().Split(“,”c)(1)
,拉出值或TransactionKey:
Public只读属性TransactionKey()作为字符串
得到
返回Merchant.GetStringValue().Split(“,”c)(1)
结束
端属性
这是最有效的方法吗?与其使用
StringValueAttribute
,不如使用字典(字符串、字符串)
创建一个属性,因为它是一个键/值对列表?还是字符串数组或列表?也许林克有什么事?或者它已经很有效了吗?我建议您创建自己的属性类,它以类型安全的方式接受这两个值,并对它们进行适当的命名。或者,如果不是每个项目都有两个值,则创建两个单独的属性,每个值一个
或者更好的是,根本不使用枚举。创建自己的类,该类接受构造函数中的所有三个值,然后为每个项创建一个具有共享属性的类,如下所示:
Public Class Merchants
Public Shared ReadOnly Property Coke() As Merchant
Get
Return _coke
End Get
End Property
Private Shared _coke = New Merchant(0, "Coke", "faj80785hq+faf=-1=-jfa+")
...
End Class
您可以使用以下自定义属性和扩展方法来获取值。需要注意的一些事项:
- 是C#,希望没问题:)
- extension methods类在静态范围内缓存MerchantID和TransactionID,因此应该非常有效
- 您可以通过调用(例如
Merchants.Coke.GetMerchantId())来获取
代码>MerchantId
- 您可以通过调用(例如,
Merchants.Coke.GetTransactionId())来获取
代码>TransactionId
商家
值是否有效,因此您可以通过调用((商家)76282).GetMerchantId()
来打破它
[AttributeUsage(AttributeTargets.Field)]
公共类MerchantDataAttribute:属性
{
public MerchantDataAttribute(字符串merchantId、字符串transactionId)
{
this.MerchantId=MerchantId;
this.TransactionId=TransactionId;
}
公共字符串商品
{
得到;
私人设置;
}
公共字符串事务ID
{
得到;
私人设置;
}
}
公共静态类MerchantExtensions
{
专用静态只读字典
_MerchantCache=缓存MerchantCache();
公共静态字符串GetMerchantId(此字段为商户)
{
return _merchantsCache[merchants].MerchantId;
}
公共静态字符串GetTransactionId(此字段为商户)
{
return _merchantsCache[merchants].TransactionId;
}
专用静态词典cacheMerchantCache()
{
返回Enum.GetValues(类型(商户))
.Cast()
.选择(m=>new
{
商户=m,
商品属性=获取商品属性(m)
})
.ToDictionary(m=>m.Merchant,m=>m.MerchantAttribute);
}
私有静态MerchantDataAttribute GetMerchantAttribute(商户)
{
退货类型(商户)
.GetMember(merchant.ToString())
.First()
.GetCustomAttributes(typeof(MerchantDataAttribute),继承:false)
.Cast()
.First();
}
}
对于未来的访问者,我想我应该发布答案的VB版本,因为这就是我标记问题的地方。另外,由于VB要求扩展位于模块内部,所以我不得不做一些稍微不同的事情
以下是模块:
(为了更易于阅读,我使用了大量的行延续。此外,为了在本例中的可读性,我导入了一些类,这样我就不必键入名称空间)
导入SomeClass
模块商品扩展
私有只读商品缓存_
As字典(商家、商品数据属性)_
=CacheMerchantsCache()
私有函数cacheMerchantCache()_
As字典(商家、商品数据属性)
返回[Enum].GetValues(GetType(商户))_
.演员(指商人)()_
.选择(功能(m)新建
{
.Merchant=m,
.MerchantAttribute=GetMerchantAttribute(m)
}) _
.ToDictionary(功能(m)m.Merchant_
功能(m)m.商品属性)
端函数
私有函数GetMerchantAttribute(商户作为商户)_
作为商品数据属性
返回GetType(商户)_
.GetMember(merchant.ToString())_
.First()_
.GetCustomAttributes(GetType(MerchantDataAttribute)_
继承:=假)_
.Cast(指商品数据属性)()_
.First()
端函数
公共函数GetMerchantId(商户作为商户)作为字符串
退货商品缓存(商户).Id
端函数
公共函数GetTransactionKey(商户作为商户)作为字符串
退货商品缓存(商户)。交易密钥
端函数
端模块
下面是exte的实现
[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();
}
}