我怎么能是指一个项目从另一个在C#? [英] How can I refer to a project from another one in c#?

查看:159
本文介绍了我怎么能是指一个项目从另一个在C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个项目,Project2的,我的解决方案。它已经有了另外一个项目可以说项目1.如何调用从项目2类和方法到PROJECT1?

I added a project, Project2, to my solution. It already had another project lets say Project 1. How can I call classes and methods from project2 into project1?

我做了什么:

我有项目1和它的解决方案。我加了2项目到项目1的解决方案。项目1和项目2都有一个命名空间命名修正。现在,我打电话给使用校正。然而,在项目1打字Project2中给了我一个错误,因为它声称它不知道它是什么。

I have Project 1 and its solution. I added Project 2 to Project 1's solution. Project 1 and Project 2 both have a namespace named Correction. Now, I called using Correction. However, in Project 1 typing Project2 gives me an error since its claiming that it does not know what it is.

我还添加项目2作为参考。

I also added project 2 as a reference.

感谢所有的答案。我不知道我做错了。

thanks for all the answers. i dont know what i am doing wrong

推荐答案

首先,你需要在PROJECT1添加到Project2的引用。

First, you need to add a reference to Project2 in Project1.

如果你去到Project - >引用 - >添加引用,您应该看到一个选项添加项目的解决方案和项目2

If you go to Project1 -> References -> Add Reference, you should see an option to add projects in solutions and add project2.

一旦你添加一个引用,来调用命名空间NAME1.NAME2一类Foo,您可以使用类

Once you add a reference, to call a class Foo in namespace Name1.Name2, you can use the class as

Name1.Name2.Foo foo = new Name1.Name2.Foo(...);

或者如果你想避免打字,你可以添加接近文件的顶部

or if you would like to avoid typing, you could add the using statement near the top of the file

using Name1.Name2;

和可以引用只使用美孚类,像

and can now reference the class using just Foo, like

Foo foo = new Foo(...);

请注意,您将拥有的找出Project2中的命名空间的。只需使用名称Project2的将无法工作。看看其中包含要使用,并查找命名空间定义的类的声明文件。

Note, you will have figure out the namespaces in Project2. Just using the name Project2 won't work. Look at the file which contains the declaration of the class you want to use and look for the namespace definition.

所以,如果你看到的东西为

So if you see something as

namespace Name1.Name2 {


    class Bar {
        // Blah
    }

    // Notice the word public here.
    public class Foo {
        // Blah
    }
}

NAME1.NAME2是Foo的命名空间,这是你需要使用的东西。

Name1.Name2 is the namespace for Foo and that is what you need to use.

另外请注意,您可能需要具备的公共的访问您要在PROJECT1使用类修饰符。例如,在上述情况下,你应该能够访问类Foo,但不是一流的酒吧。

Also note that you will likely need to have the public access modifier for the class which you want to use in Project1. For example in the above scenario, you should be able to access class Foo, but not class Bar.

这个页面将帮助您了解命名空间:<一href=\"http://www.csharp-station.com/tutorials/lesson06.aspx\">http://www.csharp-station.com/tutorials/lesson06.aspx

This page will help you understand namespaces: http://www.csharp-station.com/tutorials/lesson06.aspx

这篇关于我怎么能是指一个项目从另一个在C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆