function - 在我声明它们之后是否可以重新评估这里的字符串?

标签 function powershell parameters herestring

我正在尝试将 PowerShell 用于小型代码生成任务。我希望脚本生成一些 java 类和接口(interface)。

在脚本中,我想在这里声明一个字符串变量。例如:

$controllerContent = @"
package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Controller
@EnableWebMvc
public class $completeName {

}
"@

在声明之后,我想将它传递给计算变量 $completeName 的函数.但我不知道替换字符串中变量的正确方法是什么。我必须使用 -replace ?还是有其他方法?

最佳答案

我通常使用类似于上面答案的格式字符串,但您也可以将其包装在脚本 block 中并在需要时调用。这不需要对他的文本本身进行任何修改。前任:

$completeName = "HelloWorld"

$controllerContent = { @"
@Controller
@EnableWebMvc
public class $completeName {

}
"@ }

& $controllerContent
#Save the string:  $str = & $controlledContent

输出:
@Controller
@EnableWebMvc
public class HelloWorld {

}

更改变量:
$completeName = "HelloNorway"

& $controllerContent

输出:
@Controller
@EnableWebMvc
public class HelloNorway {

}

关于function - 在我声明它们之后是否可以重新评估这里的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36808803/

相关文章:

ajax - 在 jQuery Mobile 中通过 ajax 加载带参数的 url

php - PDO 无法识别参数

javascript - 函数的输出 = NAN

c# - 如何从 C# 程序运行 bat 文件?

powershell - 如何在一个命令中使用 'dir' 到不同的文件夹?

object - 使用 PowerShell 的 Add-Member 会导致错误

jquery - 函数返回零如何解决这个问题?

C++ 模板没有匹配的函数调用

regex - Powershell JSON 转换删除 unicode 转义字符而不删除文字\n

parameters - Keras Sequential fit_generator 参数列表中validation_steps的含义