Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9021659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:18:30+00:00 2026-06-16T05:18:30+00:00

I am currently updating some exising Silverlight code and moving helper classes into a

  • 0

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:18:31+00:00Added an answer on June 16, 2026 at 5:18 am

    The problem is, your dictionary’s TKey is System.Type, but you’re expecting to use the values of your enumeration as keys. The dictionary’s type should probably be IDictionary<object, StringValueAttribute>

    (Also, you’re using Contains instead of ContainsKey in the second version. Although the documentation does state that IDictionary.Contains should look at the keys, so I’m not entirely sure why that doesn’t do what you expect.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to separate some of my code from my main form, and
I've currently run into a performance problem when updating properties on lots of dom
I'm working updating some legacy code that does not properly handle user input. The
Currently, my entire website does updating from SQL parameterized queries. It works, we've had
I'm currently in the process of updating a (lot of) old xsl+html code to
I am currently updating a webpage that has some very simple data displayed in
I am currently reviewing some code for colleagues on a different project and they
I'm updating some of our legacy C++ code to use the MFC feature pack
I currently have some code which needs to perform multiple updates per user for
I am currently updating some of my Coldfusion applications, and I am looking for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.