【问题标题】:Using HereStrings to add an Enum type in Windows Powershell在 Windows Powershell 中使用 HereStrings 添加枚举类型
【发布时间】:2020-11-09 18:47:45
【问题描述】:

我正在使用 5.1 版的 Windows Powershell ISE 从目录中读取文件,然后希望将这些文件名添加到 Enum 以供以后使用。我找到了一种方法,通过 HereString 传入 C# 代码。

#where the files would be read
Param([Parameter(Mandatory=$True, Position=0)][ValidateNotNullOrEmpty()][String]$path)

#search for files in the path directory
$files = Get-ChildItem $path -Name

$HereString = @"
public enum Files
{
$(
  foreach($file in $files){$file}
  {
   "$file,"
  }
 )
}
"@

Add-Type -TypeDefinition $HereString

如果我不尝试传入文件名(通过声明一个简单的 Enum),则代码可以正常工作。但是这段代码会出现以下错误:

Add-Type : c:\Users\HPC\AppData\Local\Temp\1n43jzgm.0.cs(3) : } expected

c:\Users\HPC\AppData\Local\Temp\1n43jzgm.0.cs(2) : {

c:\Users\HPC\AppData\Local\Temp\1n43jzgm.0.cs(3) : >>> delete_maybe.txt, hello_world.txt, test.txt ,

c:\Users\HPC\AppData\Local\Temp\1n43jzgm.0.cs(4) : }

At c:\Users\HPC\Documents\Task Scripts\grab_sql_scripts.ps1:35 char:1

+ Add-Type -TypeDefinition $HereString

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Exception

    + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand


Add-Type : c:\Users\HPC\AppData\Local\Temp\1n43jzgm.0.cs(4) : Type or namespace definition, or end-of-file expected

c:\Users\HPC\AppData\Local\Temp\1n43jzgm.0.cs(3) : delete_maybe.txt, hello_world.txt, test.txt ,

c:\Users\HPC\AppData\Local\Temp\1n43jzgm.0.cs(4) : >>> }

At c:\Users\HPC\Documents\Task Scripts\grab_sql_scripts.ps1:35 char:1

+ Add-Type -TypeDefinition $HereString

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Exception

    + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand


Add-Type : Cannot add type. Compilation errors occurred.

At c:\Users\HPC\Documents\Task Scripts\grab_sql_scripts.ps1:35 char:1

+ Add-Type -TypeDefinition $HereString

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Add-Type], InvalidOperationException

    + FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand

我在网上找不到很多关于这个特定问题的文档。有谁知道这里发生了什么?如果没有,有没有人知道另一种方法来尝试我想要实现的目标?

谢谢!

【问题讨论】:

  • 将文件名更改为驼峰式大小写导致同样的错误。
  • 不是大小写,而是.'s
  • 什么“点”!?我在上面的示例 sn-p 中没有看到任何点!

标签: c# windows powershell


【解决方案1】:

enum 成员名称必须是符合 C# 语言规范的有效标识符

valid identifier 不能包含.,唯一允许的标点符号是连接符(即_)。

所以,要么完全省略扩展:

"$($file -replace '\..*$'),"

...或将. 替换为_

"$($file -replace '\.','_'),"

...或更新您的帖子以通过基于文件名动态编译枚举类型来解释您要实现的目标,也许我们可以向您展示一个更好的选择:)

【讨论】:

  • 非常感谢!我最终使用了带有 for 循环的 [IO.Path]::GetFileNamesWithoutExtention()。这解决了最初的问题。我正在使用另一个应用程序通过控制台远程运行服务器脚本。我只能使用 Enum 通过控制台列出这些脚本。
  • 酷,你也可以从Get-ChildItem 中省略-Name,然后在循环中引用$($file.BaseName),这可能会更容易阅读
猜你喜欢
  • 2019-05-18
  • 2017-02-14
  • 2019-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多