0

我目前正在更新一些现有的 Silverlight 代码并将帮助程序类移动到 Silverlight 5 库中,但我正在努力将 HashTable 实现更改为 IDictionary 实现,如下所示。

此功能允许对枚举进行属性化,从而允许查找字符串值。

代码编译但stringValues.Add(value, attrs[0]);在类中的行中失败,ParseEnumStrings并具有以下异常详细信息。

知道我在转换代码时做错了什么吗?

例外

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;
    }

新的字典实现

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();
4

1 回答 1

1

问题是,您的字典TKeySystem.Type,但您希望将枚举的值用作键。字典的类型可能应该是IDictionary<object, StringValueAttribute>

(另外,您在第二个版本中使用Contains而不是ContainsKey。虽然文档确实说明IDictionary.Contains应该查看密钥,所以我不完全确定为什么这不能达到您的预期。)

于 2012-12-14T13:38:34.437 回答