简体   繁体   English

指定的演员表无效。 枚举

[英]Specified cast is not valid. enum

whats wrong with this code, i try also Enum.Parse but didnt work. 这段代码有什么问题,我也尝试了Enum.Parse但没有用。

public enum RoleNames  
{
    Administrator,
    [Description("Personnel Security")]
    PrsonalSecurity,
}

foreach (RoleNames roleName in arRoles) //<<<error
{
        if (IsCurrentUserInRole(roleName)) { return true; }
}

arRoles is ArrayList of RoleNames, which is passing as a parameters. arRoles是RoleNames的ArrayList,它作为参数传递。

Can you post the rest of your code as the following example would work just fine: 您能否发布其余代码,因为以下示例可以正常工作:

public enum RoleNames   
{ 
    Administrator, 
    [Description("Personnel Security")] 
    PersonalSecurity
} 

RoleNames[] testEnumArray =
    { RoleNames.Administrator, RoleNames.PersonalSecurity };
foreach (RoleNames en in testEnumArray)
{
    // do something
}

Based on your error message, arRoles must not be an array of RoleNames since the cast is failing. 根据您的错误消息,由于RoleNames失败, arRoles不得为RoleNames数组。

If you want to iterate over your enum definition you can use the following code: 如果要遍历枚举定义,则可以使用以下代码:

foreach (RoleNames type in Enum.GetValues(typeof(RoleNames)) 
{   
    // do something
} 

Post your exact code. 发布您的确切代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM