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 77979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:54:55+00:00 2026-05-10T20:54:55+00:00

I have a piece of C# code that add the values of an enum

  • 0

I have a piece of C# code that add the values of an enum to a drop down list by type. It requires that it be called with T1 being an enum type, although I cannot specify this as a type constraint because enums are special case in which this isn’t possible. This is not a major concern as this is only used internally and is documented.

Description is an extension on System.Enum that returns the value of the DescriptionAttribute of the value of the enum or the ToString of the value if it’s not specified.

Because of this I must cast to System.Enum in the Cast part of the LINQ statement and not to T1 otherwise the Description extension isn’t available.

public static void AddEnum<T1>(this System.Web.UI.WebControls.DropDownList me) {     Type t = Enum.GetUnderlyingType(typeof(T1));     var l = Enum.GetValues(typeof(T1))                 .Cast<System.Enum>()                 .Select(x => new ListItem(x.Description(),                                           Convert.ChangeType(x, t)                                                  .ToString()));     me.Items.AddRange(l.ToArray()); } 

This is fine and works in Visual Studio 2008s web development server but fails with a cast exception when run on IIS 6. It is run with an unchanging, hardcoded enumerable as the type parameter to populate ASP pages.

System.InvalidCastException: Invalid cast from ‘%namespace.class+nameofenum%’ to ‘System.Enum’. at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) at System.Linq.Enumerable.d__b01.MoveNext() at System.Linq.Enumerable.<SelectIterator>d__d2.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source) at AddEnum[T1](DropDownList me)

(namespace/classname obscured by me)

I am unable to test it on other versions of IIS but it seems very strange that the development version doesn’t match the the production system. Does anyone know why this is?

  • 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. 2026-05-10T20:54:55+00:00Added an answer on May 10, 2026 at 8:54 pm

    Shot in the dark. One of the platforms is running 3.5 RTM and the other is running 3.5 SP1. If so you are likely running into a breaking change introduced in 3.5SP1 that altered the way .Cast() operates. See these posts for more details

    • http://blogs.msdn.com/ed_maurer/archive/2008/02/16/breaking-change-in-linq-queries-using-explicitly-typed-range-variables.aspx
    • http://blogs.msdn.com/dinesh.kulkarni/archive/2008/08/10/net-fx-3-5-sp1-two-perf-improvements-linq-to-objects-and-linq-to-sql.aspx
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a piece of code for which cccheck tells me that a Requires()
I have a piece of code that was written by someone else before the
I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I have this piece of code that works fine in subsonic 2.2, I migrated
I have a piece of code that load a very big image in memory.
I have a piece of code that takes several rows from a database and
I have a piece of code that look similar to this: <xsl:choose> <xsl:when test=some_test>
I have a piece of code that calls a WCF service that is hosted
If have a piece of code that gets some data from a sql database
I have a piece of code that converts an infix expression to an expression

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.