gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:24:44 26 4
gpt4 key购买 nike

我在PowerShell中创建了以下枚举。但是,如果枚举名称中有一个点(例如“Name.A”),则Add-Type将返回错误。我该怎么办?我不想从“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或添加说明,或者其他您真正不应该做的奇怪的事情)

关于c# - PowerShell:枚举名称中的点导致添加类型失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39881884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com