简体   繁体   English

如何分配在2个不同名称空间中声明的相同类对象

[英]How to assign same class object declared in 2 different namespaces

I have a webservice project with a class (let's refer to it as webservice.classA). 我有一个带有类的Web服务项目(让我们将其称为webservice.classA)。

I have another class project producing a dll which references that class in its own namespace and instantiates an instance of it (lets call the dlls namespace dllnamespace). 我有另一个类项目,该类项目生成一个dll,该dll在其自己的名称空间中引用该类并实例化它的一个实例(我们将其称为dlls名称空间dllnamespace)。

In another project I want to access the member in the dll 在另一个项目中,我要访问dll中的成员

eg 例如

using webservice;

namespace other_project
{
    class B
    {
        classA copy = null;
        //....
        dllnamespace.dostuff(); // amongst other things instantiates a classA object
        //....
        copy = dllnamespace.getclassA(); // method to return classA member

The compiler error I get is cannot convert type from dllnamespace.webservice.classA to other_project.webservice.classA 我得到的编译器错误是无法将类型从dllnamespace.webservice.classA转换为other_project.webservice.classA

I guess I have a fundamental design flaw but I figure there must be (?) a way to declare/use "webservice.classA" in more than one namespace. 我猜我有一个基本的设计缺陷,但是我认为必须有(?)在多个名称空间中声明/使用“ webservice.classA”的方法。

You have a name clash. 您有名字冲突。 The supported way of avoiding this (short of not naming your classes the same), is to define a using alias for one of the classes: 避免这种情况的一种受支持的方式(不要为您的类命名相同)是为其中一个类定义using别名

using webservice.classA = myWebserviceClassA;

You are right...the design flaw does exist in terms of naming. 您说得对...在命名方面确实存在设计缺陷。

Let us assume: 让我们假设:

  • you have a class named MyClass 您有一个名为MyClass的类

  • the class exists both in namespace- abc.xyz.qwe.tyu.MyClass 该类在命名空间中存在abc.xyz.qwe.tyu.MyClass

  • and in namespace - sed.qwe.dfg.ert.MyClass 并在命名空间中sed.qwe.dfg.ert.MyClass

The workaround is - 解决方法是-

using NS1 = abc.xyz.qwe.tyu.MyClass;
using NS2 = sed.qwe.dfg.ert.MyClass;

This way you avoid the clash. 这样可以避免冲突。 Also, helpful to use if you have very long namespaces. 另外,如果名称空间很长,很有用

FURTHER REFERENCE : (From MSDN article on using Directive ) 进一步的参考:(摘自有关using Directive的 MSDN文章)

  • The scope of a using directive is limited to the file in which it appears. using指令的范围仅限于出现该指令的文件。

  • Create a using alias to make it easier to qualify an identifier to a namespace or type. 创建using别名,以使将标识符限定为名称空间或类型变得更加容易。

  • Create a using directive to use the types in a namespace without having to specify the namespace. 创建using指令以使用名称空间中的类型,而不必指定名称空间。 A using directive does not give you access to any namespaces that are nested in the namespace you specify. using指令不能使您访问嵌套在指定名称空间中的任何名称空间。

将复制定义行更改为:

dllnamespace.webservice.classA copy = null;

That's just the problem - you cannot have a class in more than one namespace. 仅仅是问题-您不能在一个以上的命名空间中拥有一个类。 This is what namespaces were designed for - to prevent classes with the same name written by different people from aliasing. 这就是名称空间的设计目的-防止不同人编写的具有相同名称的类出现别名。 You'll need to decide for one of your namespaces to own that class and in the other one to import it. 您需要确定一个名称空间拥有该类,而另一个名称空间则要导入它。 Alternatively if the dll and the web service are part of the same distributed app then they should use the same namespace. 或者,如果dll和Web服务是同一分布式应用程序的一部分,则它们应使用相同的名称空间。

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

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