SGEN reflective type

I implemented the change mentioned in the accepted answer Create an Xml serialization assembly as part of my assembly

<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 when creating exe project:

Error 14 An error has occurred reflecting the type "myNamespace.myAssembly.myForm.MicroContact". C: \ dev \ src \ myClient \ myClient \ SGEN myClient

Here is the code for MicroContact (nothing is unique 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 a way to get an internal build error exception?

+4
source share
2 answers

As Mark Gravell noted, running sgen /v MyClient.exe in the bin directory provided additional information.

The problem is caused by the fact that several classes use the same name, in this case two forms implement the same MicroContact class, because one of them was copied from the other.

+6
source

As mentioned in the previous answer - most often the problem is duplication of type names. However, the solutions to the problem are different:

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

If you solved this problem differently, I would like to know about it.

0
source

Source: https://habr.com/ru/post/1379943/


All Articles