我将值传递给T4Scaffolding.Scaffolder的前两个参数。但它不起作用,我除外。
这是powershell脚本文件。让我们给它命名为Test.ps1
[T4Scaffolding.Scaffolder(Description = "Enter a description of Service here")][CmdletBinding()]
param(
[string]$ClassName,
[string]$Project,
[string]$CodeLanguage,
[string[]]$TemplateFolders,
[switch]$Force = $false
)
Write-Host "Class name is " $ClassName
Write-host "Project name is" $Project
然后我在包管理器控制台上运行它,如下所示
PM> Scaffold Test Member ConsoleApplication1.Domain
它正确打印了ClassName,但是项目名称是在nuget包管理器控制台上选择的项目名称,而不是我给出的名称。
Class name is Member
Project name is ConsoleApplication1.Scaffolder
但例外结果是
Class name is Member
Project name is ConsoleApplication1.Domain
现在的问题是如何将ClassName和Project作为参数传递?它只需要ClassName作为参数,但忽略第二个。
EDITED
也尝试了这个,但它也没有用。
Scaffold Service -ClassName Member -Project ConsoleApplication1.Domain
答案 0 :(得分:0)
在删除T4Scaffolding属性后,从powershell控制台调用脚本,名为Scaffold.ps1,如下所示:
.\Scaffold Test Member ConsoleApplication1.Domain
将返回
Class name is Test
Project name is Member
这是预期的。我不确定你为什么说你会得到不同的结果,但即使你的预期结果也不可预期。
这样称呼它:
Scaffold Service -ClassName Member -Project ConsoleApplication1.Domain
是否给出了预期的结果
Class name is Member
Project name is ConsoleApplication1.Domain
$ CodeLanguage将是服务
所以也许是NuGet包管理器控制台做了不同的事情
答案 1 :(得分:0)
默认的$ Project参数就像您已经说明在PMC中选择的项目一样。
要将代码绑定到另一个项目(或添加新项目),您可以这样做......
[T4Scaffolding.Scaffolder(Description = "Description here...")][CmdletBinding()]
param([parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)][string]$ProjectName = "",
[string]$Project,
[string]$CodeLanguage,
[string[]]$TemplateFolders,
[switch]$Force = $false)
请注意,将通过属性名称从管道中检索“ProjectName”......以下是。
Scaffold Service -projectname SomeProjectName
将$ ProjectName的值设置为“SomeProjectName”..
在此之后,您可以使用此名称执行任何操作...如果项目已存在,则将代码添加到项目中,或者如果项目尚未在解决方案中,则将其添加到项目中。
示例:
#
#Add new project if it does not exist
#
if(($DTE.Solution.Projects | Select-Object -ExpandProperty Name) -notcontains $ProjectName){
Write-Host "Adding new project"
$sln = [System.IO.Path]::GetFilename($dte.DTE.Solution.FullName)
$path = $dte.DTE.Solution.FullName.Replace($sln,'').Replace('\\','\')
$sln = Get-Interface $dte.Solution ([EnvDTE80.Solution2])
$templatePath = $sln.GetProjectTemplate("ClassLibrary.zip","CSharp")
$sln.AddFromTemplate($templatePath, $path+$ProjectName,$ProjectName)
$file = Get-ProjectItem "Class1.cs" -Project $ProjectName
$file.Remove()