两个相同的类位于不同的库和一个项目中
我有两个相同的类,具有不同的属性并且位于不同的库中。 我在项目中引用这两个库,当我尝试访问第二个库的属性时,出现错误“无法解析符号”。 如果其中一类“设置”被重命名为其他内容,则一切正常。编译器似乎搜索第一类中的属性并忽略第二个同名的类。我尝试用部分属性装饰类以将类合并为一个类,但似乎只有当类位于同一个库中时这才有效。有什么建议吗?
Lib1.dll
namespace caLibServer
{
public partial class Settings
{
public static string MyProperty1
//skip code
}
}
Lib2.dll
namespace caLibClient
{
public partial class Settings
{
public static string MyProperty2
//skip code
}
}
I have two identical classes with different properties and in different libraries.
I reference these two libraries in my project and when I try to access properties of second library I get error "Cannot resolve symbol".
If one of classes Settings are renamed to something else everything works fine. It seems that compiler search for properties in first class and ignore the second class with the same name. I tried to decorate class with partial attribute to merge class into one but it seems this works only when classes are in the same library. Any suggestion?
Lib1.dll
namespace caLibServer
{
public partial class Settings
{
public static string MyProperty1
//skip code
}
}
Lib2.dll
namespace caLibClient
{
public partial class Settings
{
public static string MyProperty2
//skip code
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要使用命名空间来访问第二个类:
您使用的
partial
关键字是无用的,因为这是两个不同的类,只是碰巧具有相同的名称。他们没有关系。You need to use the namespace to access the second one:
The
partial
keyword you are using is useless, because these are two distinct classes that just happen to have the same name. They are not related.如果您想让一个类拥有所有属性,而不是两个不同的类,您可以使用继承来实现这一目的。
例如。
现在您可以:
否则您可以使用类的完整路径来指定您想要的类。
例如。
这取决于您想要实现的目标。
If you want to have one class having all the properties instead of two distinct classes you could use inheritance to do so.
eg.
And now you can have:
Otherwise you can use the full path to the class to specify which one you mean.
eg.
It depends on what you are trying to accomplish.
尝试使用相同的命名空间。您可以更改某个库的默认命名空间。
Try using the same namespace. You can change the default namespace for one of your libraries.
这种说法没有多大意义。
如果它们是两个相同的类,那么它们不会具有不同的属性,不同的事实使它们独一无二。
除了您认为两个不同的类(甚至不相互继承)可以以生成此错误的方式使用这一明显事实之外
我有几个你可能不喜欢的:-)
This statement does not make a great deal of sense.
If they are two identical classes, then they would not have different properties, the fact there are different makes them unique.
You need to post the code for us to explain what exactly you are doing wrong besides the obvious fact you think two different classes ( not even inherited with one another ) can used in a way that generates this error.
I have a couple you just wouldn't like them :-)
使用命名空间完全限定类名。您还可以更轻松地在文件之间进行引用,请使用
使用
关键字。Fully qualify the class name with the namespace. You can also make it easier to reference from file to file, use the
using
keyword.这取决于尝试使用它们的 C# 文件中的 using 语句。
仅假设:
尝试
Settings.MyProperty1
时,它将在caLibServer
中看到该值。与“only”类似:
Settings.MyProperty1
将仅看到caLibServer
中的那个。如果同时拥有两者(无论顺序如何):
Settings.MyProperty1
将完全失败,您必须指定caLibServer.Settings.MyProperty1
或Settings.MyProperty1
代码>.不过,您可以为每个设置设置别名,方法是:
然后您可以使用
Server_Settings.MyProperty1
或Client_Settings.MyProperty1
等别名(假设您将使用以下别名)长度比原始命名空间短,我在这里使用下划线来表明这些不是常用的名称,但您不必这样做唯一的要求是将它们放在命名空间内。定义)。This depends on the using statements in your C# files that try to use them.
Assuming only:
When trying
Settings.MyProperty1
it'll see the one incaLibServer
.Similarly with "only":
Settings.MyProperty1
will see only the one incaLibServer
.If having both (regardless of order):
Settings.MyProperty1
will fail completely, you'll have to specifycaLibServer.Settings.MyProperty1
orSettings.MyProperty1
.You can set aliases to each of them though, by doing:
Then you can use the aliasses like
Server_Settings.MyProperty1
orClient_Settings.MyProperty1
(Assuming you'll use an alias that is less in length than original namespaces, I here use underscore to make it obvious those are not usual names, but you don't have to do this. The only requirement is having them inside the namesepace definition).