4

作为我的构建的一部分,我已经实现了生成 Xml 序列化程序集的公认答案中提到的更改

<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)" BuildAssemblyPath="$(OutputPath)" References="@(ReferencePath)" ShouldGenerateSerializer="true" UseProxyTypes="false" KeyContainer="$(KeyContainerName)" KeyFile="$(KeyOriginatorFile)" DelaySign="$(DelaySign)" ToolPath="$(SGenToolPath)" Platform="$(Platform)">
      <Output TaskParameter="SerializationAssembly" ItemName="SerializationAssembly" />
   </SGen>
</Target>

构建 exe 项目的错误消息:

错误 14 出现反映类型“myNamespace.myAssembly.myForm.MicroContact”的错误。C:\dev\src\myClient\myClient\SGEN myClient

这是 MicroContact 的代码(这里没有什么独特之处):

Public Class MicroContact
    Implements IComparable

    Private _id As Long
    Private _name As String

    Public Property Id() As Long
        Get
            Return _id
        End Get
        Set(ByVal value As Long)
            _id = value
        End Set
    End Property

    Public Property NoTitleFullName() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Public Sub New()
        _name = ""
    End Sub

    Public Sub New(ByVal id As Long, ByVal name As String)
        _id = id
        _name = name
    End Sub

    Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
        Return String.Compare(Me.NoTitleFullName, CType(obj, MicroContact).NoTitleFullName, True)
    End Function

End Class

有什么方法可以让我获得构建错误的内部异常吗?

4

2 回答 2

6

正如 Marc Gravell 指出的那样sgen /v MyClient.exe,在 bin 目录中运行会产生更多信息。

问题是由多个共享相同名称的类引起的,在这种情况下,两个表单都实现了相同的 MicroContact 类,因为一个是从另一个复制而来的。

于 2011-11-07T07:14:24.213 回答
0

如上一个答案所述 - 最常见的问题是重复的类型名称。然而,该问题的解决方案是多种多样的:

  • 更改其中一种重复类型的名称或通过更改其 xml 序列化名称[XmlType("NewTypeName")]
  • 声明[System.Xml.Serialization.XmlType(AnonymousType = true)]序列化类型的属性。
  • 为其中一种重复类型定义命名空间 - 例如,如果它用于 XML 元素类型,则使用[XmlElement(Namespace="http://example.com")]

如果您以另一种方式解决了这个问题 - 我想知道它。

于 2016-07-21T10:00:06.600 回答