我正在使用 T4Scaffolding 创建自定义脚手架。我使用以下 PS 代码获取项目中所有域对象的列表:
# List of all domain classes. Get all top level files/folders in the project | drill down to Models folder | Enumerate ProjectItems | Where Name ends with .cs | Select name truncating .cs, pluralized name
$domainClasses = (Get-Project "Domain").ProjectItems | Where { $_.Name -eq "Models" } | ForEach { $_.ProjectItems } | Where { $_.Name.EndsWith('.cs') } | Select @{ Name = 'Name'; Expression={ $_.Name.SubString(0,$_.Name.Length - 3) } }, @{ Name = 'Plural'; Expression={ Get-PluralizedWord $_.Name.SubString(0,$_.Name.Length - 3) } }
if (!$domainClasses) { $domainClasses = @() }
然后我像这样调用 Add-ProjectItemViaTemplate 方法:
Add-ProjectItemViaTemplate $outputPath -Template MyTemplate `
-Model @{ DomainClasses=[Array]$domainClasses } `
-SuccessMessage "Added Domain output at {0}" `
-TemplateFolders $TemplateFolders -Project $DomainProjectName -CodeLanguage $CodeLanguage -Force:$Force
运行 sacffold 时出现以下异常:
System.Runtime.Serialization.SerializationException: Type 'System.Management.Automation.PSCustomObject' in assembly 'System.Management.Automation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.
问题似乎是 $domainClasses 变量由于某种原因无法序列化。我究竟做错了什么?