如何在.Net中同一应用程序中考虑不同项目的类 [英] How Can I Accsess Classes Of Different Project In Same Application In .Net

查看:65
本文介绍了如何在.Net中同一应用程序中考虑不同项目的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在同一解决方案中的不同项目中使用类和对象。

是否可以使用导入命名空间?

How can i use classes and objects in different projects in same solution.
Is it work with importing namespace ?

推荐答案

添加对包含它们的项目的引用,然后在要引用该命名空间的代码顶部添加using指令。

嘿,它们可以访问类/对象(只要它们是公共的) 。
Add a reference to the project that contains them and then add using directive at the top of the code where you want to reference that namespace.
Hey presto the classes/objects (aslong as they are public) should accessible.


是 - 并且没有。

首先添加对项目的引用,该项目包含您要在其中使用的项目中的类。

然后,您可以使用 Imports (VB)或使用(C#)语句来简化引用。



请注意,如果它会创建循环引用链,则无法添加引用:如果您尝试,VS会抱怨,因为无法构建解决方案。在这种情况下,您需要创建一个新的程序集,其中包含来自两个项目的公共代码和引用。
Yes - and no.
First add a reference to the project containing the class in the project you want to use it in.
You can then use the Imports (VB) or using (C#) statement to make referencing easier.

Do note that you can't add a reference if it would create a circular reference chain: VS will complain if you try because it would become impossible to build the solution. In that case you need to create a new assembly which contains the common code and reference that from both the otehr projects.


命名空间与类型的导入或可访问性无关。 项目与类型的可访问性无关。



一切都发生在组装层面。 Project只是编译成程序集的源代码。如果你有两个组件,一个可以引用另一个组件;这意味着它的名称(或强名称)在引用程序集的PE文件中提到。引用某个程序集时,其 public 声明(类型和成员,以及成员的 protected )变得完全可访问就像它们在同一个程序集中一样可以访问。



这是有关使用程序集(和命名空间)的信息: http://msdn.microsoft.com/en-us/library/ms973231.aspx [ ^ ]。



许多初学者有关命名空间的问题,想知道使用声明无法使用程序集。也许在MSDN中没有解释得那么好;请仔细阅读。你真的不需要使用;它只是为了方便和速记命名而编写的;你总是使用完整的类型名称,你可以使用在没有的情况下开发。这仅适用于顶级类型的命名:它们可以具有相同的简单名称,并且仍然由完全限定的名称区分。



现在,如果您放置所有相关项目对于相同的解决方案(强烈推荐),您可以使用添加引用窗口的项目选项卡通过项目引用。尽管最终引用是通过引用程序集的主可执行模块的名称完成的(最典型的是* .DLL,但它始终可以是* .EXE或带有.NET程序集的任何其他文件)表现)。这种方法大大改善了开发过程,原因有很多。



你可以在这里了解模块和.NET中的角色:

http://msdn.microsoft.com/en-us/ library / 168k2ah5%28v = vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/7d3c18c2%28v=vs.110%29.aspx [ ^ ]。



-SA
Namespaces have nothing to do with importing or accessibility of types. "Projects" have nothing to do with accessibility of types.

Everything happens at the level of assemblies. Project is just source code which is compiled into assembly. If you have two assemblies, one can reference another one; it means that its name (or strong name) is mentioned in the PE file of referencing assembly. When some assembly is referenced, its public declarations (types and members, but also protected for members) become exactly as accessible as accessible as if they were in the same assembly.

This is information on using assemblies (and namespaces): http://msdn.microsoft.com/en-us/library/ms973231.aspx[^].

Many beginners asked questions about namespace wondering that using declarations cannot help to use assemblies. Maybe something is not so well explained in MSDN; just read it more carefully. You don't really need using; it is written just for convenience and shorthand naming; it you always used full type names, you could develop without using. This is just for naming of top-level types: they can have identical simple name and still differentiated by fully-qualified names.

Now, if you put all related projects to the same solution (highly recommended), you can reference by projects, using the tab "Projects" of the window "Add Reference". Even though ultimately the reference is done by the name of the main executable module of the referenced assembly (most typically, *.DLL, but it always can be *.EXE or any other file with .NET assembly manifest). This approach greatly improves development process, by a number of reasons.

You can get an idea about modules and there role in .NET here:
http://msdn.microsoft.com/en-us/library/168k2ah5%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/7d3c18c2%28v=vs.110%29.aspx[^].

—SA


这篇关于如何在.Net中同一应用程序中考虑不同项目的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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