简体   繁体   English

在C#中添加对项目的引用

[英]Add reference to project in c#

I have some strange problem. 我有一些奇怪的问题。 I have a solution with the following structure 我有以下结构的解决方案

http://i33.tinypic.com/10fbzbq.jpg http://i33.tinypic.com/10fbzbq.jpg

As you can see when i wonna import the VDB.Common.RequestAndResponses it gives an error. 如您所见,当我要导入VDB.Common.RequestAndResponses时,它将产生错误。 The namespace of dat Class Library is VDB.Common.RequestAndResponses. dat类库的名称空间是VDB.Common.RequestAndResponses。

I am new in c# , so it could be that i forgot something stupid. 我是c#的新手,所以可能是我忘记了一些愚蠢的东西。

I strongly suspect that Base.cs (the only C# file shown in the VDB.Common.RequestAndResponses project) doesn't actually declare a type in the VDB.Common.RequestAndResponses namespace - or that it only declares an internal (rather than public ) type. 我强烈怀疑Base.cs (中所示的只有C#文件VDB.Common.RequestAndResponses项目) 实际上并没有宣布在a型VDB.Common.RequestAndResponses命名空间-或者说,它只是声明了一个内部 (而非公共 )类型。

For example, note that the code you're creating is under the VDB.Client.Infrastructure project, but is only declaring a class in the Agatha namespace - not VDB.Client.Infrastructure.Agatha , which may be what you were intending. 例如,请注意,您正在创建的代码在VDB.Client.Infrastructure项目下,但是仅在Agatha命名空间中声明一个类-而不是VDB.Client.Infrastructure.Agatha ,这可能正是您想要的。 Do you have the same kind of thing in Base.cs , perhaps? 也许在Base.cs有同样的事情?

Without seeing the code in Base.cs, we can't see what's wrong though. 没有看到Base.cs中的代码,我们看不到出了什么问题。 If you could just post a snippet of that - just the namespace and class declaration - that would be helpful. 如果您只可以发布一个代码段-仅命名空间和类声明-将会很有帮助。

Note that although a class library has a default namespace, this isn't prepended to whatever the source file actually declares. 请注意,尽管类库具有默认的名称空间,但它并不在源文件实际声明的名称之前。 In other words, in a library of Acme.Widgets , if you had a declaration of: 换句话说,在Acme.Widgets库中,如果您声明:

namespace Web
{
    public class Button {}
}

that would only declare the type Web.Button , not Acme.Widgets.Web.Button . 这只会申报类型Web.Button Acme.Widgets.Web.Button

EDIT: The OP's "answer" confirms what I thought... basically it's not declaring a namespace at all. 编辑:OP的“答案”证实了我的想法...基本上,它根本没有声明名称空间。 It should look like this: 它应该如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agatha.Common;

namespace VDB.Common.RequestAndResponses
{
    public abstract class BaseRequest :Request
    {
        // Code
    }

    public abstract class BaseResponse : Response
    {
        // Code
    }
}

I would also strongly advise that these classes should be put in two separate files, BaseRequest.cs and BaseResponse.cs . 我也强烈建议将这些类放在两个单独的文件BaseRequest.csBaseResponse.cs I'm also pretty surprised to see a reference to Agatha.Common - shouldn't that be VDB.Common.Agatha or something like that? 我也很惊讶地看到对Agatha.Common的引用-那不应该是VDB.Common.Agatha或类似的东西吗?

在解决方案资源管理器中的“ VDB.Common.RequestAndResponses”引用上单击鼠标右键,然后选择“在对象浏览器中显示”,确保在该名称空间中找到准确的拼写和大写字母。

Try to use the Base class in the client code and hover over it and allow the Visual Studio IDE to prompt you to add the appropriate namespace. 尝试在客户端代码中使用Base类,并将其悬停在上面,并允许Visual Studio IDE提示您添加适当的名称空间。 The namespace defined in the Base class could be different to what you think. 在基类中定义的名称空间可能与您所想的不同。

EDIT As Jon as demonstrated in the 2nd part of his answer - the name of the code file does not automatically correspond to the namespace. 编辑正如乔恩在回答第二部分中所展示的那样-代码文件的名称不会自动对应于名称空间。

The Base.cs file looks like this. Base.cs文件如下所示。

using System;

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Agatha.Common;

    public abstract class BaseRequest :Request
        {
        public string UserName { get; set; }
        public string UserDomainName { get; set; }

        public string ClientLanguageCode { get; set; }
        public DateTime ClientCreated { get; set; }
        public DateTime ClientSent { get; set; }
        public DateTime ServerReceived { get; set; }
        public DateTime ServerProcessed { get; set; }

        public void BeforeSend(IUserContext context)
        {
            ClientSent = DateTime.UtcNow;
            UserName = context.UserName;
            UserDomainName = context.UserDomainName;
            ClientLanguageCode = context.LanguageCode;
        } 
    }

public abstract class BaseResponse : Response
{
    public DateTime ServerCreated { get; set; }
    public DateTime ServerProcessed { get; set; }

    public string[] ValidationErrors { get; set; }

    public bool IsValid
    {
        get { return Exception == null & !ValidationErrors.Any(); }
    }

}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM