c# - PowerShell enum value that represents all -


given current enum:

add-type -typedefinition @"    [system.flags]     public enum flagsenum {       none = 0,       summaryinfo = 1,       reportoptions = 2,       parameterfields = 4    } "@ 

is there way create entry sets bits 1? syntax causes errors:

add-type -typedefinition @"    [system.flags]     public enum flagsenum {       none = 0,       summaryinfo = 1,       reportoptions = 2,       parameterfields = 4,       = (summaryinfo -bor reportoptions -bor parameterfields)    } "@ 

** edit **

changed declaration:

add-type -typedefinition @"    [system.flags]     public enum flagsenum {       none = 0,       summaryinfo = 1,       reportoptions = 2,       parameterfields = 4,       = (summaryinfo | reportoptions | parameterfields)    } "@ 

the code:

$flags = [flagsenum]::all  if ( $flags -band [flagsenum]::summaryinfo ) { write-host "add summaryinfo" }  if ( $flags -band [flagsenum]::reportoptions ) { write-host "add reportoptions" }  if ( $flags -band [flagsenum]::parameterfields ) { write-host "add parameterfields" }  

results:

add-type : cannot add type. type name 'flagsenum' exists. @ c:\documents , settings\xxx\my documents\windowspowershell\scripts\enums.ps1:3 char:9 + add-type <<<<  -typedefinition @"     + categoryinfo          : invalidoperation: (flagsenum:string) [add-type], exception     + fullyqualifiederrorid : type_already_exists,microsoft.powershell.commands.addtypecommand 

try this:

add-type -typedefinition @"   [system.flags]    public enum flagsenum {     none = 0,     summaryinfo = 1,     reportoptions = 2,     parameterfields = 4,     = (summaryinfo | reportoptions | parameterfields)   } "@ 

remember inside here doc, you're not writing powershell, you're writing c#.

edit posterity based on comments asker of original question

the actual error being shown stated type existed. because earlier version of code had been run in same powershell session. unfortunate limitation of powershell can't add type, tweak , re-add (which makes developing scripts define own types painful).


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -