Dan Field는이며 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 ([email protected])
# 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 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();
Array []가 포함 된 줄을 List으로 간단하게 바꿀 수 있습니까? 아니면 편집 된 코드의 다른 줄도 있습니까? –
Gero
고정 배열이 포함 된 라인 만. 이 도구는 C# -source를 파싱하고 NRefactory를 사용하여 AST (Abstract Syntax Tree)로 전송합니다. 따라서 변경 사항은 강력한 방식으로 적용됩니다. 아이디어는 가능한 한 적은 수의 원본을 만지는 것이 었습니다. – EvilBad
예, 최신 버전을 업로드하십시오. – Gero