简体   繁体   English

当我没有放置命名空间时,我的类有什么命名空间?

[英]What namespace are my classes in when I don't put a namespace?

if I don't put a namespace in my classes (vs.net 2008), which namespace do my classes get built under? 如果我没有在我的类中添加命名空间(vs.net 2008),我的类是在哪个命名空间下构建的?

update Its strange, when I add a namespace I can't seem to reference the classes methods from my user control. 更新它很奇怪,当我添加一个命名空间时,我似乎无法从我的用户控件引用类方法。

If I explicitly set a namespace, and add a 'using ...' in my control, I still can't see it! 如果我明确设置一个命名空间,并在我的控件中添加一个'using ...',我仍然看不到它!

How can this be? 怎么会这样?

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
/// <summary>
/// Summary description for Globals
/// </summary>
public class Globals
{
    public Globals()
    { }


    public static string ConnectionString
    {
        get
        {
            return ConfigurationManager.ConnectionStrings["MyDb"].ConnectionString;
        }
    }

}

My control: 我的控制:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class MyStats : System.Web.UI.UserControl
{




    protected void Page_Load(object sender, EventArgs e)
    {


    //etc.

If you do not provide a namespace then your type has no namespace. 如果您不提供命名空间,那么您的类型没有命名空间。 Remember that namespaces don't have any meaning post-compile. 请记住,命名空间在编译后没有任何意义。 They simply get appended to the beginning of you type's name to create a longer name that has a greater probability of being unique. 它们只是附加到您键入名称的开头,以创建一个更有可能是唯一的更长名称。

Edit: I think that you may have two separate assemblies and one ought to be referencing the other but it is not. 编辑:我认为你可能有两个独立的程序集,一个应该引用另一个,但事实并非如此。 If two projects are in a single solution then the class viewer will show all types from all projects but that does not necessarily mean that ProjectA references ProjectB . 如果两个项目在一个解决方案中,那么类查看器将显示所有项目中的所有类型,但这并不一定意味着ProjectA引用了ProjectB In order to use the types from ProjectB in ProjectA you need to ensure that a project reference exists. 要在ProjectA使用ProjectB中的类型,您需要确保存在项目引用。

as you said, that class will not have any namespace, It will be accessible without a using clause. 如你所说,该类没有任何命名空间,没有using子句就可以访问它。

Namespaces is a feature for putting order on your classes, by classifying, mostly according to their funcionality 命名空间是一种功能,通过分类,主要根据其功能,对您的类进行排序

EDIT 编辑

According to your update, i think your "global" class maybe putting trouble because of the global:: clause... would you mind to change the name of the class just to see if it works? 根据你的更新,我认为你的“全局”类可能会因为global :: clause而引起麻烦...你是否想改变类的名称只是为了看它是否有效?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ActiveX组件无法创建对象:在注册dll时没有名称空间的类会发生什么情况? - ActiveX component can't create object: what happens to classes that don't have a namespace when the dll is registered? 嵌套类没有命名空间 - Nested classes don't have a namespace 如何使其与 class 位于同一命名空间中的类可以访问变量,但命名空间之外的类不在 C# 中? - How do I make it so classes in the same namespace as a class have access to a variable but classes outside of a namespace don't in C#? 为什么我在Visual Studio中的对象浏览器中找不到System.Windows.Controls命名空间? - Why don't I find the System.Windows.Controls namespace in my object browser in Visual Studio? 为什么我不需要在测试类文件中添加我的主项目的命名空间? - Why don't I need to add my main project's namespace in the test class files? 我设置了命名空间,但没有得到正确的xml - I set the namespace but I I don't get the correct xml 我应该将我的接口定义放在与其实现相同的命名空间中吗 - Should I put my interface definition in same namespace as its implementation 数组返回我的命名空间和类型,但不是我输入的值 - Array is returning my Namespace and Type but not the Values that i put in 将项目部署到生产服务器时,找不到名称空间System.DirectoryServices - When I deploy my project to my production server, the namespace System.DirectoryServices isn't found 如何选择将生成的C#类放入的名称空间? - How do I select namespace into which generated C# classes are put?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM