VS#8和IIS6之间的C#强制转换不同

时间:2021-04-20 20:58:30

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.

我有一段C#代码,它按类型将枚举值添加到下拉列表中。它要求在T1为枚举类型的情况下调用它,尽管我不能将其指定为类型约束,因为枚举是特殊情况,这是不可能的。这不是主要问题,因为它仅在内部使用并记录在案。

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.

Description是System.Enum的扩展,它返回枚举值的DescriptionAttribute的值或值的ToString(如果未指定)。

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.

因此,我必须在LINQ语句的Cast部分中转换为System.Enum而不是T1,否则说明扩展不可用。

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.

这很好,可以在Visual Studio 2008s Web开发服务器中运行,但在IIS 6上运行时失败并出现强制转换异常。它使用一个不变的,硬编码的可枚举作为填充ASP页面的类型参数运行。

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)

System.InvalidCastException:从'%namespace.class + nameofenum%'到'System.Enum'的转换无效。在System.Convert.DefaultToType(IConvertible值,类型TARGETTYPE,的IFormatProvider提供商)在System.Convert.ChangeType(对象值,类型conversionType,的IFormatProvider提供商)在System.Linq.Enumerable.d__b01.MoveNext()在System.Linq.Enumerable d__d2.MoveNext()在System.Linq.Buffer1..ctor(IEnumerable1源)在System.Linq.Enumerable.ToArray [TSource](IEnumerable`1源)在AddEnum [T1](DROPDOWNLIST我)

(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?

我无法在其他版本的IIS上测试它,但是开发版本与生产系统不匹配似乎很奇怪。有人知道为什么吗?

1 个解决方案

#1


5  

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

在黑暗中拍摄。其中一个平台运行3.5 RTM,另一个运行3.5 SP1。如果是这样,你可能会遇到3.5SP1中引入的改变方式.Cast()的运行方式。有关详细信息,请参阅这些帖子

#1


5  

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

在黑暗中拍摄。其中一个平台运行3.5 RTM,另一个运行3.5 SP1。如果是这样,你可能会遇到3.5SP1中引入的改变方式.Cast()的运行方式。有关详细信息,请参阅这些帖子