抛出异常的TestRunType的序列化

LB4*_*B40 4 c# xml-serialization trx c#-4.0

我正在尝试分析一些trx文件(webTestResults)来输出合成的excel文件.

首先,我使用trx xsd模式(visual studio目录中的vstst.xsd)生成一堆C#类.

然后,我尝试基于TestRunType的类型(从模式生成)实例化XmlSerializer.

XmlSerializer xmlSer = new XmlSerializer(typeof(TestRunType));
Run Code Online (Sandbox Code Playgroud)

XMLSerializer实例化引发了一个异常:

System.InvalidOperationException: There was an error reflecting type 'TestRunType'. ---> System.InvalidOperationException: There was an error reflecting property 'Items'. ---> System.InvalidOperationException: There was an error reflecting type 'TestRunTypeTestDefinitions'. ---> System.InvalidOperationException: There was an error reflecting property 'Items'. ---> System.InvalidOperationException: There was an error reflecting type 'OrderedTestType'. ---> System.InvalidOperationException: There was an error reflecting type 'CodedWebTestElementType'. ---> System.InvalidOperationException: There was an error reflecting property 'Items'. ---> System.InvalidOperationException: Member 'CodedWebTestElementType.Items' hides inherited member 'BaseTestType.Items', but has different custom attributes.
   at System.Xml.Serialization.StructMapping.FindDeclaringMapping(MemberMapping member, StructMapping& declaringMapping, String parent)
   at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)
Run Code Online (Sandbox Code Playgroud)

什么是自定义属性?只是BaseTestType的开头:

public abstract partial class BaseTestType {

    private object[] itemsField;

    private bool enabledField;

    private string idField;

    private string nameField;

    private bool isGroupableField;

    private int priorityField;

    private string namedCategoryField;

    private string storageField;

    public BaseTestType() {
        this.enabledField = true;
        this.isGroupableField = true;
        this.priorityField = 2147483647;
        this.namedCategoryField = "";
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Agent", typeof(BaseTestTypeAgent))]
    [System.Xml.Serialization.XmlElementAttribute("Css", typeof(BaseTestTypeCss))]
    [System.Xml.Serialization.XmlElementAttribute("DeploymentItems", typeof(BaseTestTypeDeploymentItems))]
    [System.Xml.Serialization.XmlElementAttribute("Description", typeof(object))]
    [System.Xml.Serialization.XmlElementAttribute("Execution", typeof(BaseTestTypeExecution))]
    [System.Xml.Serialization.XmlElementAttribute("Owners", typeof(BaseTestTypeOwners))]
    [System.Xml.Serialization.XmlElementAttribute("Properties", typeof(BaseTestTypeProperties))]
    [System.Xml.Serialization.XmlElementAttribute("TcmInformation", typeof(TcmInformationType))]
    [System.Xml.Serialization.XmlElementAttribute("TestCategory", typeof(BaseTestTypeTestCategory))]
    [System.Xml.Serialization.XmlElementAttribute("WorkItemIDs", typeof(WorkItemIDsType))]
    public object[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
Run Code Online (Sandbox Code Playgroud)

和CodedWebTestElementType:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://microsoft.com/schemas/VisualStudio/TeamTest/2010")]
public partial class CodedWebTestElementType : BaseTestType {

    private object[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("IncludedWebTests", typeof(CodedWebTestElementTypeIncludedWebTests))]
    [System.Xml.Serialization.XmlElementAttribute("WebTestClass", typeof(CodedWebTestElementTypeWebTestClass))]
    public object[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释问题是什么吗?

谢谢...

Nik*_* G. 8

解决方案是从派生类(CodedWebTestElementTypeGenericTestType)中删除Items属性,但也要将序列化属性移动到基类,以便在您进行编码Web测试或通用测试时不会遗漏这些值.

IOW,解决方案如下.

首先,ItemsCodedWebTestElementType类型中删除属性

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://microsoft.com/schemas/VisualStudio/TeamTest/2010")]
public partial class CodedWebTestElementType : BaseTestType {
}
Run Code Online (Sandbox Code Playgroud)

然后,将它的两个XmlElementAttribute属性移动到BaseTestType基类(参见最后两个):

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Agent", typeof(BaseTestTypeAgent))]
[System.Xml.Serialization.XmlElementAttribute("Css", typeof(BaseTestTypeCss))]
[System.Xml.Serialization.XmlElementAttribute("DeploymentItems", typeof(BaseTestTypeDeploymentItems))]
[System.Xml.Serialization.XmlElementAttribute("Description", typeof(object))]
[System.Xml.Serialization.XmlElementAttribute("Execution", typeof(BaseTestTypeExecution))]
[System.Xml.Serialization.XmlElementAttribute("Owners", typeof(BaseTestTypeOwners))]
[System.Xml.Serialization.XmlElementAttribute("Properties", typeof(BaseTestTypeProperties))]
[System.Xml.Serialization.XmlElementAttribute("TcmInformation", typeof(TcmInformationType))]
[System.Xml.Serialization.XmlElementAttribute("TestCategory", typeof(BaseTestTypeTestCategory))]
[System.Xml.Serialization.XmlElementAttribute("WorkItemIDs", typeof(WorkItemIDsType))]
[System.Xml.Serialization.XmlElementAttribute("IncludedWebTests", typeof(CodedWebTestElementTypeIncludedWebTests))]
[System.Xml.Serialization.XmlElementAttribute("WebTestClass", typeof(CodedWebTestElementTypeWebTestClass))]
public object[] Items
{
    get {
        return this.itemsField;
    }
    set {
        this.itemsField = value;
    }
}
Run Code Online (Sandbox Code Playgroud)

之后,为GenericTestType班级做同样的事情.

这样,如果您有一天获得IncludedWebTests,WebTestClass,CommandSummaryXmlFile节点,您将不会丢失信息.