PowerShell:枚举名称中的点导致添加类型失败

PowerShell: Dot in Enum Name Causing Add-Type to Fail

我在下面的 PowerShell 中创建了枚举。但是,如果我在枚举名称中有一个点(例如 "Name.A"),那么 Add-Type 将 return 出错。我该怎么做?我不想从 "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__

这是我得到的错误:

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

这是 .Net 的限制,以及 PowerShell 和 C# 的扩展。枚举成员名称中不能有标点符号。

因此简短的回答是,如果不做一些骇人听闻的事情就无法完成这项工作(您必须将 name.A 更改为 name[CharacterCode]A 或添加描述,或其他一些你真的不应该做的奇怪的事情)