开发者

Specified cast is not valid. enum

开发者 https://www.devze.com 2023-01-18 12:31 出处:网络
whats wrong with this code, i try a开发者_运维百科lso Enum.Parse but didnt work. public enum RoleNames

whats wrong with this code, i try a开发者_运维百科lso Enum.Parse but didnt work.

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.


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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消