Dan Field 有一个 PowerShell 脚本,它采用xsd.exe输出类并将其数组转换为通用列表。这对我来说很适合一个简单的类,但我不知道它的扩展性如何。我已经粘贴了下面的脚本。从命令提示符调用它,如下所示:
"$(TargetFrameworkSDKToolsDirectory)xsd.exe" /c "$(ProjectDir)ImportedPartCanonical.xsd" "$(ProjectDir)ProjectCanonical.xsd" /n:Tallan.BT.PipelineComponents
powershell.exe -ExecutionPolicy Unrestricted -file "$(solutiondir)\PowerShellScripts\PostProcessXsdExe.ps1" ProjectCanonical.cs "$(SolutionDir)Tallan.BT.PipelineComponents\SerializedClasses\ProjectCanonical.cs"
有关完整说明,请参阅链接。
# Author: Dan Field (dan.field@tallan.com)
# posted on blog.tallan.com/2016/03/10/xsd-exe-arrays-and-specified
# Purpose: fix the 'specified' attribute and convert arrays to list from XSD.exe generated classes
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, Position=1)]
[string]$inputFile,
[Parameter(Mandatory=$true, Position=2)]
[string]$outputFile,
[switch]$DeleteInputFile
)
# Much faster than using Get-Content and/or Out-File/Set-Content
$writer = [System.IO.StreamWriter] $outputFile
$reader = [System.IO.StreamReader] $inputFile
# Used to track Specified properties
$setterDict = @{}
while (($line = $reader.ReadLine()) -ne $null)
{
$thisStart = $line.IndexOf("this.") # Will be used for
$brackets = $line.IndexOf("[]") # Indicates an array that will be converted to a Generic List
# Assume that any private field that contains "Specified" needs to be grabbed
if (($line.IndexOf("private") -gt -1) -and ($line.IndexOf("Specified") -gt -1))
{
# Get the field name
$varName = $line.Split("{' ', ';'}", [System.StringSplitOptions]::RemoveEmptyEntries)[-1]
# Use the field name as a key, minus the ending "Specified" portion, e.g. fieldNameSpecified -> fieldName
# The value in the dictionary will be added to setters on the main property, e.g. "this.fieldNameSpecified = true;"
$setterDict.Add($varName.Substring(0, $varName.IndexOf("Specified")), "this." + $varName + " = true;")
# Output the line as is
$writer.WriteLine($line)
}
# Find property setters that aren't for the *Specified properties
elseif (($thisStart -gt -1) -and ($line.IndexOf(" = value") -gt -1) -and ($line.IndexOf("Specified") -lt 0))
{
# Get the field name
$thisStart += 5
$varName = $line.Substring($thisStart, $line.IndexOf(' ', $thisStart) - $thisStart)
# see if there's a "Specified" property for this one
if ($setterDict.ContainsKey($varName) -eq $true)
{
# Set the Specified property whenever this property is set
$writer.WriteLine((' ' * ($thisStart - 5)) + $setterDict[$varName])
}
# Output the line itself
$writer.WriteLine($line)
}
elseif ($brackets -gt 0) # change to List<T>
{
$lineParts = $line.Split(' ')
foreach ($linePart in $lineParts)
{
if ($linePart.Contains("[]") -eq $true)
{
$writer.Write("System.Collections.Generic.List<" + $linePart.Replace("[]", "> "))
}
else
{
$writer.Write($linePart + " ")
}
}
$writer.WriteLine();
}
else # Just output the original line
{
$writer.WriteLine($line)
}
}
if ($DeleteInputFile -eq $true)
{
Remove-Item $inputFile
}
# Make sure the file gets fully written and clean up handles
$writer.Flush();
$writer.Dispose();
$reader.Dispose();