sgen.exe 如何决定为哪些类型生成序列化程序?

发布于 2024-11-25 01:57:52 字数 124 浏览 3 评论 0原文

您可以使用 sgen.exe 提前创建序列化程序集,而不是即时创建它们。

但它如何决定为哪些类型创建序列化器呢?我尝试改用 sgen,但它一直告诉我它找不到任何适用的类型。是否有必须添加的属性?或者我需要手动指定类型?

You can use sgen.exe to create serialization assemblies ahead of time rather than have them created on the fly.

But how does it decide what types to make serializers for? I've tried to switch to using sgen, but it's been telling me that it can't find any applicable types. Is there an attribute you have to add? Or will I need to manually specify the types?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-小熊_ 2024-12-02 01:57:52

我猜这是因为类型不是公共的 - XML 序列化仅适用于公共类型。

I'd guess this is because the types weren't public - XML serialisation only works with public types.

奢望 2024-12-02 01:57:52

VS触发的sgen(即通过属性->构建->生成序列化程序集)实际上没有生成序列化程序集的最常见原因是因为VS中默认的Sgen构​​建任务默认包含/proxytypes开关,并且有没有办法把它关掉。此开关告诉 sgen 仅生成 Xml Web 服务代理类型的序列化程序,如文档 这里。要生成其他类型的序列化程序,您可以通过添加自己的自定义任务来覆盖默认行为,如 生成 Xml 序列化程序集作为我的构建的一部分。您可以更进一步,仅指定您希望通过 Types 参数生成序列化代码的类型,例如:

<PropertyGroup>
<!-- Specific set of types to create serialization assemblies for -->
<SerializationTypes>
    Type1;
    Type2
</SerializationTypes>
</PropertyGroup>
<!-- Custom non-proxy SGEN invocation to create pre-compiled serialization assemblies -->
<Target Name="GenerateNonProxySerializationAssemblies"
DependsOnTargets="AssignTargetPaths;Compile;ResolveKeySource"
Inputs="$(MSBuildAllProjects);@(IntermediateAssembly)"
Outputs="$(OutputPath)$(_SGenDllName)">
    <SGen BuildAssemblyName="$(TargetFileName)" Types="$(SerializationTypes)"
        BuildAssemblyPath="$(OutputPath)" References="@(ReferencePath)"
        ShouldGenerateSerializer="true" UseProxyTypes="false"
        KeyContainer="$(KeyContainerName)" KeyFile="$(KeyOriginatorFile)"
        DelaySign="$(DelaySign)" ToolPath="$(SGenToolPath)">
        <Output TaskParameter="SerializationAssembly" ItemName="SerializationAssembly" />
    </SGen>
</Target>

The most common reason for VS-triggered sgen (i.e. via Properties->Build->Generate serialization assembly) to not actually produce a serialization assembly is because the default Sgen build task in VS includes the /proxytypes switch by default, and there is no way to turn it off. This switch tells sgen to only generate serializers for Xml Web service proxy types, as documented here. To generate serializers for other types, you can override the default behavior by adding your own custom task as described in Generating an Xml Serialization assembly as part of my build. You can go further and specify only the types you want serialization code generated for via the Types parameter, e.g.:

<PropertyGroup>
<!-- Specific set of types to create serialization assemblies for -->
<SerializationTypes>
    Type1;
    Type2
</SerializationTypes>
</PropertyGroup>
<!-- Custom non-proxy SGEN invocation to create pre-compiled serialization assemblies -->
<Target Name="GenerateNonProxySerializationAssemblies"
DependsOnTargets="AssignTargetPaths;Compile;ResolveKeySource"
Inputs="$(MSBuildAllProjects);@(IntermediateAssembly)"
Outputs="$(OutputPath)$(_SGenDllName)">
    <SGen BuildAssemblyName="$(TargetFileName)" Types="$(SerializationTypes)"
        BuildAssemblyPath="$(OutputPath)" References="@(ReferencePath)"
        ShouldGenerateSerializer="true" UseProxyTypes="false"
        KeyContainer="$(KeyContainerName)" KeyFile="$(KeyOriginatorFile)"
        DelaySign="$(DelaySign)" ToolPath="$(SGenToolPath)">
        <Output TaskParameter="SerializationAssembly" ItemName="SerializationAssembly" />
    </SGen>
</Target>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文