SGEN error reflecting type

3.9k views Asked by At

I have implemented the change mentioned in the accepted answer of Generating an Xml Serialization assembly as part of my build

<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>

Error message on building the exe project:

Error 14 There was an error reflecting type 'myNamespace.myAssembly.myForm.MicroContact'. C:\dev\src\myClient\myClient\SGEN myClient

Here's the code for MicroContact (there's nothing unique in here):

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

Is there any way I can get the inner exception of a build error perhaps?

2

There are 2 answers

1
Seph On BEST ANSWER

As Marc Gravell pointed out running sgen /v MyClient.exe in the bin directory yielded more information.

The problem was caused by multiple classes sharing the same name, in this case two forms both implemented the same MicroContact class as one was copied from the other.

0
vezenkov On

As mentioned in the previous answer - the problem most often is duplicated type names. However the solutions to the problem are various:

  • Change the name of one of the duplicated type or change its xml serialization name through [XmlType("NewTypeName")]
  • Declare [System.Xml.Serialization.XmlType(AnonymousType = true)] attribute for the serialized type.
  • Define a namespace for one of the duplicated type - for example if it's used for an XML element type then use [XmlElement(Namespace="http://example.com")]

If you solved this problem in another way - I'd like to know about it.