【问题标题】:Unit Tests: The type "Status" exists in two projects单元测试:“状态”类型存在于两个项目中
【发布时间】:2016-04-04 06:49:27
【问题描述】:

我正在为我的程序编写单元测试,但遇到了两个不同项目中存在的我的类型错误。我试图通过在代码中添加一个项目名称来克服它:var status = CommunicationsServer.Status() 但它说那里不存在 Status() 类型。

请帮忙

这里是 CommunicationsServer.Status 类的前几行:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.mini.pw.edu.pl/ucc/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.mini.pw.edu.pl/ucc/", IsNullable=false)]
public partial class Status {

    private ulong idField;

    private StatusThread[] threadsField;

    /// <remarks/>
    public ulong Id {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }
//further code 
}

【问题讨论】:

  • 如果没有CommunicationsServerTaskManager 中两种类型的额外上下文,就很难确定解决方案。为什么不在CommunicationsServer 库中添加using 语句?如果是静态类,请将using static 放在命名空间和静态类型之前。如果您实际输入 var status = CommunicationsServer.Status() 它也可能不是静态类型,并且 VS 不知道您在引用什么。请提供有关冲突类型(命名空间、类声明等)的更多信息,您可能会得到更好的帮助。
  • 上下文是什么意思? CommunicationServer 和 TaskManager 都是包含 public partial class Status 类型的 Status 类的项目,我为该类创建了一个构造函数,我不应该需要我的类是静态的。 using语句如何实现?
  • 我用更多信息编辑了我的问题
  • 在您编辑后,我还有一些问题。这个状态类是从一些 XML 到 C# 工具自动生成的吗?文件顶部的命名空间是什么?
  • 是:using CommunicationsServer;

标签: c# .net types


【解决方案1】:

如果您有两种不同的类型,它们必须用命名空间分隔。如果您有两个项目ProjectOneProjectTwo,具有相同的类型MyType,那么实际上没有办法区分类型。我不确定它在下面是如何工作的,但我能够实现这一点,并且无法让 Visual Studio 编译我对第三个项目 ProjectThree 中的类型的引用。与您的问题完全相同。

出于某种原因,自动生成的 xml 到 C# 文件并不总是在它生成的类周围放置一个命名空间。只需提供一个与项目相关的唯一命名空间,您就可以开始了。

在您的 CommunicationsServer 生成的文件中:

namespace CommunicationsServer.Xsd 
{
    public partial class Status() 
    {

    }
}

并在TaskManager生成的文件中:

namespace TaskManager.Xsd 
{
    public partial class Status() 
    {

    }
}

然后在 ProjectThree 中,您可以放置​​ using 语句 using CommunicationsServer.Xsd 或完全限定类型:var status = new CommunciationsServer.Xsd.Status();

【讨论】:

  • 天哪!有用!非常感谢您的时间和精力
猜你喜欢
  • 1970-01-01
  • 2013-06-25
  • 1970-01-01
  • 2011-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-30
相关资源
最近更新 更多