我目前正在更新一些正在升級的Silverlight代碼並將幫助器類移動到Silverlight 5庫中,但我正在爲從HashTable實現到IDictionary實現的更改而掙扎,如下所示。從Silverlight HashTable到IDictionary的聚合問題
此功能允許列舉歸因允許查找字符串值。
代碼編譯但在ParseEnumStrings
類的stringValues.Add(value, attrs[0]);
行中失敗,但具有以下異常詳細信息。
任何想法,我已經做了錯誤的代碼轉換?
異常
The value "Today" is not of type "System.Type" and cannot be used in this generic collection.
Parameter name: key.
at System.ThrowHelper.ThrowWrongKeyTypeArgumentException(Object key, Type targetType)
at System.Collections.Generic.Dictionary`2.System.Collections.IDictionary.Add(Object key, Object value)
at Silverlight.Helper.Enums.ParseEnumStrings.GetStringValue(Enum value) in Z:\Perforce\Development\Microsoft .Net\dotNet 4.0\Silverlight 5\Helper\Helper\Enums\ParseEnumStrings.cs:line 25
at QSmartFaultsByZone.Web.Models.QSmartService.GetRTF(Int32 buID, String zones, ReportTimePeriod time) in Z:\Perforce\Development\Microsoft .Net\dotNet 4.0\Silverlight 5\QSmart Faults By Zone\QSmartFaultsByZone.Web\Models\QSmartService.cs:line 114
at GetRTF(DomainService , Object[])
at System.ServiceModel.DomainServices.Server.ReflectionDomainServiceDescriptionProvider.ReflectionDomainOperationEntry.Invoke(DomainService domainService, Object[] parameters)
at System.ServiceModel.DomainServices.Server.DomainOperationEntry.Invoke(DomainService domainService, Object[] parameters, Int32& totalCount)
at System.ServiceModel.DomainServices.Server.DomainService.Query(QueryDescription queryDescription, IEnumerable`1& validationErrors, Int32& totalCount)
枚舉
public enum ReportTimePeriod
{
[StringValue("Today")]
Today = 0,
[StringValue("Twenty Four Hours")]
TwentyFourHours = 1,
[StringValue("Week")]
Week = 2,
[StringValue("Month")]
Month = 3
}
字符串值屬性
public class StringValueAttribute : Attribute
{
private readonly string value;
public StringValueAttribute(string value)
{
this.value = value;
}
public string Value
{
get { return this.value; }
}
}
傳統Hashtable類
public class ParseEnumStrings
{
private static Hashtable _stringValues = new Hashtable();
public static string GetStringValue(Enum value)
{
string output = null;
Type type = value.GetType();
//Check first in our cached results...
if (_stringValues.ContainsKey(value))
output = (_stringValues[value] as StringValueAttribute).Value;
else
{
//Look for our 'StringValueAttribute'
//in the field's custom attributes
FieldInfo fi = type.GetField(value.ToString());
StringValueAttribute[] attrs =fi.GetCustomAttributes(typeof(StringValueAttribute),false) as StringValueAttribute[];
if (attrs.Length > 0)
{
_stringValues.Add(value, attrs[0]);
output = attrs[0].Value;
}
}
return output;
}
新的IDictionary實現
public class ParseEnumStrings
{
private static IDictionary stringValues = new Dictionary<Type, StringValueAttribute>();
public static string GetStringValue(Enum value)
{
string result = string.Empty;
Type type = value.GetType();
if (stringValues.Contains(value))
result=(stringValues[value] as StringValueAttribute).Value;
else
{
FieldInfo f = type.GetField(value.ToString());
StringValueAttribute[] attrs = f.GetCustomAttributes(typeof(StringValueAttribute), false) as StringValueAttribute[];
if (attrs.Length > 0)
{
stringValues.Add(value, attrs[0]);
result = attrs[0].Value;
}
}
return result;
}
}
實施
return (from rft in qs.spBusinessProductsRFTTodayV2(zones, buID, ParseEnumStrings.GetStringValue(time))
select new RightFirstTimeReportDto
{
Builds=rft.Builds,
BuildsWithFaults=rft.BuildsWithFaults,
Name=rft.Name,
RightFirstTime=rft.RFT,
ZoneID=rft.ZoneID
}).ToList();
問題是,你的字典的'TKey'是'System.Type',但你期望使用枚舉的值作爲鍵。字典的類型可能應該是IDictionary
修復了添加問題,但突出顯示了另一個問題。 IDictionary上的contains方法是否像HashTable一樣工作?它沒有找到現有的項目。 –
注意你在原始版本中如何使用'ContainsKey',但在新版本中使用'Contains'。 – Rytmis