I am currently updating some exising Silverlight code and moving helper classes into a Silverlight 5 library but I am struggling with the change from a HashTable implementation to a IDictionary implementation as shown below.
This functionality allows the enumerations to be attributed allowing the looking up of a string value.
The code compiles but fails on the stringValues.Add(value, attrs[0]); line in the ParseEnumStrings class with the below exception details.
Any idea what I have done wrong in the conversion of the code?
Exception
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)
Enum
public enum ReportTimePeriod
{
[StringValue("Today")]
Today = 0,
[StringValue("Twenty Four Hours")]
TwentyFourHours = 1,
[StringValue("Week")]
Week = 2,
[StringValue("Month")]
Month = 3
}
String Value Attribute
public class StringValueAttribute : Attribute
{
private readonly string value;
public StringValueAttribute(string value)
{
this.value = value;
}
public string Value
{
get { return this.value; }
}
}
Legacy HashTable class
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;
}
New IDictionary implementation
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;
}
}
Implementation
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();
The problem is, your dictionary’s
TKeyisSystem.Type, but you’re expecting to use the values of your enumeration as keys. The dictionary’s type should probably beIDictionary<object, StringValueAttribute>(Also, you’re using
Containsinstead ofContainsKeyin the second version. Although the documentation does state thatIDictionary.Containsshould look at the keys, so I’m not entirely sure why that doesn’t do what you expect.)