c# - 如何使用自定义命名空间?

标签 c# namespaces mono

在 Mono 基础中,有一个例子:

using System;
using System.Windows.Forms;

public class HelloWorld : Form
{
    static public void Main ()
    {
        Application.Run (new HelloWorld ());
    }

    public HelloWorld ()
    {
        Text = "Hello Mono World";
    }
}

我想知道如何在 C# 代码中添加自定义命名空间?例如,添加一个新的命名空间

using myComponent;



为了附加一个新的字符串,

myString.FirstString



编译时会报错:
using System;
using System.Windows.Forms;
using myComponents;

public class HelloWorld : Form
{
    static public void Main ()
    {
        Application.Run (new HelloWorld ());
    }

    public HelloWorld ()
    {
        Text = "Hello Mono World " + myString.FirstString;
    }
}

helloworld.cs(3,7): error CS0246: The type or namespace name 'myComponent' could not be found (are you missing a using directive or an assembly reference?)



下面是自定义的“myComponent”,如何将 Main() 和“myComponent”集成在一起?
namespace myComponent
{
    public static class myString
    {
        public static string FirstString
        {
            get
            {
                return "connection";
            }
        }
    } //Class end
}//Namespace end

我该怎么办?即使我在两个文件中使用相同的命名空间(myComponet),我仍然收到错误消息。

谢谢 !!

提供更多信息
1.两个cs文件在同一个目录下

2.执行进度如下

C:\Users\xxxxx\CSharpWorkSpace\Pop20b>csc helloworld.cs -r:System.Windows.Form
dll
Microsoft (R) Visual C# 编译器版本 2.6.0.62309 (d3f6b8e7)
版权所有 (C) 微软公司。版权所有。

警告 CS1668:指定了无效的搜索路径“C:\Program Files (x86)\sql11\LIB”
在“LIB 环境变量”中——“目录不存在”
helloworld.cs(3,7):错误 CS0246:类型或命名空间名称“myComponent”可能
找不到(您是否缺少 using 指令或程序集引用?)

C:\Users\xxxxx\CSharpWorkSpace\Pop20b>mono --version
Mono JIT 编译器版本 5.10.0(Visual Studio 内置 Mono)
版权所有 (C) 2002-2014 Novell, Inc、Xamarin Inc 和贡献者。 www.mono-proj
ect.com

最佳答案

如果已经定义了命名空间,您是否只能使用命名空间。您可以通过在该命名空间中创建类、结构、枚举或其他对象,或包含具有该命名空间的程序集来实现。

如果你把它放在你的代码中,你的使用将起作用:

namespace myComponents
{
    public class SomeComponent
    {
    }
}

关于c# - 如何使用自定义命名空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49316401/

相关文章:

c# - 如何在 C# 的泛型类型参数中使用 Switch...Case?

xaml - 使用关键字将 clr 命名空间映射到 xaml 命名空间

namespaces - 是否可以使用模块入口点而不是类来创建 Rack 中间件?

c# - .NET 开源是 Linux 和 Mac 上可用的 MS 运行时吗?

c# - 在 Linux 上使用 Mono 进行 native P/调用 : DllNotFound

c# - 如果URL不包含现有 Controller ,则404永远不会达到Application_Error

C# 文件类型关联

visual-studio - 找不到资源文件 "**/*.resx"。 (MSB3552) 在 VS for Mac 上

c# - Windows 电话 8.1 IRC

.net - VB.NET 在运行时确定项目的导入命名空间