![](/img/trans.png)
[英]Compiler error:The type or namespace name 'Core' does not exist in the namespace 'System' (are you missing an assembly reference?)
[英]Compiler complaining on a missing namespace which is the name of the class
我有以下几点:
// Generator.cs
namespace MRP
{
public class Generator
{
public enum ModeGeneration
{
ByRequest,
ByCommit
}
}
}
// CustomerOrderWrapper.cs
namespace MRP
{
class CustomerOrderWrapper
{
readonly ModeGeneration _mode;
}
}
为什么我收到一个错误消息,即找不到命名空间名称'ModeGeneration'。 这两个类都在MRP名称空间下,为什么编译器抱怨'ModeGeneration'名称空间?
再次阅读错误消息。 它应该说类似“找不到类或命名空间ModeGeneration”之类的信息,这是正确的,没有名为ModeGeneration
类/命名空间,也许您想要Generator.ModeGeneration
?
作为内部类, Generator.ModeGeneration
是整个类型名称,您不能忽略外部类名称,因为您可以在C#中定义一些具有相同名称的内部类,如下所示:
namespace Foo
{
class One
{
public class Bar { }
}
class Another
{
public class Bar { }
}
}
您可以看到Bar
模棱两可。 您应该改用One.Bar
和Another.Bar
。
是的,它们在同一个名称空间中,但enum ModeGeneration
也位于Generator
类中。 所以:
enum
放在Generator
类之外,并Generator
使用 要么
Generator.ModeGeneration
CustomerOrderWrapper.cs中的前缀ModeGeneration
- Generator.ModeGeneration
那是因为您的枚举是在类中定义的。 更改代码,如下所示:
namespace MRP
{
class CustomerOrderWrapper
{
readonly Generator.ModeGeneration _mode;
}
}
有两种方法可以解决此问题
方法1)您可以使用放在枚举前面的类名称
namespace MRP
{
public class Generator
{
public enum ModeGeneration
{
ByRequest,
ByCommit
}
}
}
// CustomerOrderWrapper.cs
namespace MRP
{
class CustomerOrderWrapper
{
readonly Generator.ModeGeneration _mode;
}
}
方法2)您可以将枚举从类中带到命名空间
namespace MRP
{
public enum ModeGeneration
{
ByRequest,
ByCommit
}
public class Generator
{
}
}
// CustomerOrderWrapper.cs
namespace MRP
{
class CustomerOrderWrapper
{
readonly ModeGeneration _mode;
}
}
希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.