2
votes

I have created enums below in PowerShell. However, if I have a dot in the enum name (for example, "Name.A") then Add-Type would return an error. How do I go about it? I don't want to remove the dot from "Name.A".

$TypeEnum = "
    namespace Types {
        public enum Id { 
            Name.A = 1,
            NameB = 2,
            NameC = 3
        }   
    }"

Add-Type -TypeDefinition $TypeEnum -Language CSharpVersion3

([Types.Id]::'Name.A').value__

Here is the error that I get:

Add-Type : c:\Users\User1\AppData\Local\Temp\ab0z5i1y.0.cs(4) : } expected c:\Users\User1\AppData\Local\Temp\ab0z5i1y.0.cs(3) :
public enum Id { c:\Users\User1\AppData\Local\Temp\ab0z5i1y.0.cs(4) : >>> Name.A = 1, c:\Users\User1\AppData\Local\Temp\ab0z5i1y.0.cs(5) :
NameB = 2, At line:10 char:1 + Add-Type -TypeDefinition $TypeEnum -Language CSharpVersion3 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (c:\Users\User1...513: } expected:CompilerError) [Add-Type], Exception + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : c:\Users\User1\AppData\Local\Temp\ab0z5i1y.0.cs(8) : Type or namespace definition, or end-of-file expected c:\Users\User1\AppData\Local\Temp\ab0z5i1y.0.cs(7) : }
c:\Users\User1\AppData\Local\Temp\ab0z5i1y.0.cs(8) : >>> } At line:10 char:1 + Add-Type -TypeDefinition $TypeEnum -Language CSharpVersion3 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (c:\Users\User1...f-file expected:CompilerError) [Add-Type], Exception + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : Cannot add type. There were compilation errors. At line:10 char:1 + Add-Type -TypeDefinition $TypeEnum -Language CSharpVersion3 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException + FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand Unable to find type [Types.Id]: make sure that the assembly containing this type is loaded. At line:12 char:1 + ([Types.Id]::'Name.A').value__ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (Types.Id:TypeName) [], RuntimeException + FullyQualifiedErrorId : TypeNotFound

1
Thats not valid C#.Seth Flowers
Can you please elaborate as in what needs to change, etc.?TDN
C# do not allow dot to be part of identifier.user4003407
Change Name.A to NameA. Periods are not allowed in identifiers in C#. Why do you not want to remove the dot?Chris Dunaway
Because the Id is fetched in to me, but I just thought of something which is to remove the dot from the name before getting the enum value.TDN

1 Answers

3
votes

This is a limitation of .Net, and by extension PowerShell and C#. Enum member names can't have punctuation in them.

Thus the short answer is, you can't make this work without doing something hacky (You'd have to change name.A to name[CharacterCode]A or add a description, or some other weird thing you really shouldn't do)