sgen.exe, XmlSerializers.dll not being used

2019-08-26 23:28发布

问题:

I need to produce XML serializers for certain of my classes in advance to avoid them being built when the application runs.

I managed to (eventually) get sgen running as a post build operation. It appears to be working as it produces the expected DLL.

However its not being used. I have configured the diagnostics to leave the files on disk and I can see them still being produced.

This is with an existing class (ClassA) of moderate complexity. So I created a simple class (ClassB) and tested on that which worked fine. My so called simple class had a reference to another class, and a list of another class within it.

Strangely if I try and compile ClassA & ClassB neither work - only when its ClassB alone does ClassB work, and ClassA never works.

I assume there is something about ClassA which can't be serialized in advance, but if so how do I find out what the issue is? Its not throwing any errors, and it is producing a DLL.

Here is my build configuration.

  <ItemGroup>
    <SgenTypes Include="GK.Data.XmlSerialTest" /> <!-- ClassB - works but only by itself -->
    <!--<SgenTypes Include="GK.Data.CalendarInfo" />--><!-- ClassA (desired class) Uncomment to test -->
  </ItemGroup>
  <Target Name="AfterBuild" DependsOnTargets="AssignTargetPaths;Compile;ResolveKeySource" Inputs="$(MSBuildAllProjects);@(IntermediateAssembly)" Outputs="$(OutputPath)$(_SGenDllName)">
    <!-- Delete the file because I can't figure out how to force the SGen task. -->
    <Delete Files="$(TargetDir)$(TargetName).XmlSerializers.dll" ContinueOnError="true" />
    <SGen BuildAssemblyName="$(TargetFileName)" Types="@(SgenTypes)" BuildAssemblyPath="$(OutputPath)" References="@(ReferencePath)" ShouldGenerateSerializer="true" UseProxyTypes="false" KeyContainer="$(KeyContainerName)" KeyFile="$(KeyOriginatorFile)" DelaySign="$(DelaySign)" ToolPath="$(TargetFrameworkSDKToolsDirectory)" Platform="$(Platform)">
      <Output TaskParameter="SerializationAssembly" ItemName="SerializationAssembly" />
    </SGen>
  </Target>