单元测试:“状态"类型存在于两个项目中 [英] Unit Tests: The type "Status" exists in two projects
问题描述
我正在为我的程序编写单元测试,但在两个不同的项目中遇到了我的类型错误.我试图通过在代码中添加一个项目名称来克服它:var status = CommunicationsServer.Status()
但它说那里不存在 Status() 类型.
I'm writing unit tests for my program and I encountered an error of my type existing in two different projects. I tried to overcome it by adding a project name to the code: var status = CommunicationsServer.Status()
but then it says that there exist no Status() type there.
请帮忙
以下是 CommunicationsServer.Status 类的前几行:
Here are the first few lines of CommunicationsServer.Status class:
[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
}
推荐答案
如果您有两种不同的类型,它们必须由命名空间分隔.如果您有两个项目,ProjectOne
和 ProjectTwo
,具有相同的类型 MyType
,那么实际上无法区分类型.我不确定它在下面是如何工作的,但我能够实现这一点,并且无法让 Visual Studio 编译我对第三个项目 ProjectThree
中的类型的引用.正如您在问题中所提出的那样.
If you have two different types, they must be separated by a namespace. If you have two projects, ProjectOne
and ProjectTwo
, with the same type MyType
, then there is literally no way to differentiate the type. I'm not sure how it works underneath, but I was able to implement this, and unable to get Visual Studio to compile my reference to the type in a third project ProjectThree
. Exactly as you have in your question.
出于某种原因,自动生成的 xml 到 C# 文件并不总是在它生成的类周围放置命名空间.只需提供一个与项目相关的唯一命名空间,您就可以开始使用了.
For some reason, auto-generated xml to C# files do not always put a namespace around the classes that it generates. Simply provide a unique namespace, relevant to the project and you should be good to go.
在您的 CommunicationsServer 生成的文件中:
In your CommunicationsServer generated file:
namespace CommunicationsServer.Xsd
{
public partial class Status()
{
}
}
在TaskManager生成的文件中:
And in the TaskManager generated file:
namespace TaskManager.Xsd
{
public partial class Status()
{
}
}
然后在 ProjectThree
中,您可以放置 using 语句 using CommunicationsServer.Xsd
或完全限定类型:var status = new CommunciationsServer.Xsd.Status();
Then in ProjectThree
you can either put a using statement using CommunicationsServer.Xsd
or fully qualify the type: var status = new CommunciationsServer.Xsd.Status();
这篇关于单元测试:“状态"类型存在于两个项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!