两个相同的类位于不同的库和一个项目中

发布于 2024-12-03 04:33:04 字数 593 浏览 1 评论 0原文

我有两个相同的类,具有不同的属性并且位于不同的库中。 我在项目中引用这两个库,当我尝试访问第二个库的属性时,出现错误“无法解析符号”。 如果其中一类“设置”被重命名为其他内容,则一切正常。编译器似乎搜索第一类中的属性并忽略第二个同名的类。我尝试用部分属性装饰类以将类合并为一个类,但似乎只有当类位于同一个库中时这才有效。有什么建议吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一枫情书 2024-12-10 04:33:04

您需要使用命名空间来访问第二个类:

caLibClient.Settings.MyProperty2

您使用的 partial 关键字是无用的,因为这是两个不同的类,只是碰巧具有相同的名称。他们没有关系。

You need to use the namespace to access the second one:

caLibClient.Settings.MyProperty2

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.

活泼老夫 2024-12-10 04:33:04

如果您想让一个类拥有所有属性,而不是两个不同的类,您可以使用继承来实现这一目的。

例如。

 namespace caLibServer
    {
        public class Settings
        {
            public virtual static string MyProperty1
             //skip code
        }
    }

 namespace caLibClient
    {
        public class Settings : caLibServer.Settings
        {
            public static string MyProperty2
             //skip code
        }
    }

现在您可以:

using caLibClient;

Settings.MyProperty1;
Settings.MyProperty2;

否则您可以使用类的完整路径来指定您想要的类。

例如。

caLibServer.Settings.MyProperty1;
caLibClient.Settings.MyProperty2;

这取决于您想要实现的目标。

If you want to have one class having all the properties instead of two distinct classes you could use inheritance to do so.

eg.

 namespace caLibServer
    {
        public class Settings
        {
            public virtual static string MyProperty1
             //skip code
        }
    }

 namespace caLibClient
    {
        public class Settings : caLibServer.Settings
        {
            public static string MyProperty2
             //skip code
        }
    }

And now you can have:

using caLibClient;

Settings.MyProperty1;
Settings.MyProperty2;

Otherwise you can use the full path to the class to specify which one you mean.

eg.

caLibServer.Settings.MyProperty1;
caLibClient.Settings.MyProperty2;

It depends on what you are trying to accomplish.

瘫痪情歌 2024-12-10 04:33:04

尝试使用相同的命名空间。您可以更改某个库的默认命名空间。

Try using the same namespace. You can change the default namespace for one of your libraries.

痴情换悲伤 2024-12-10 04:33:04

我有两个具有不同属性的相同类,并且在
不同的库。

这种说法没有多大意义。

如果它们是两个相同的类,那么它们不会具有不同的属性,不同的事实使它们独一无二。

我在我的项目中引用了这两个库,当我尝试
访问第二个库的属性时出现错误“无法解析
符号”。

除了您认为两个不同的类(甚至不相互继承)可以以生成此错误的方式使用这一明显事实之外

您还需要为我们发布代码来解释您到底做错了什么。

有什么建议吗?

我有几个你可能不喜欢的:-)

I have two identical classes with different properties and in
different 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.

I reference these two libraries in my project and when I try to
access properties of second library I get error "Cannot resolve
symbol".

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.

Any suggestion?

I have a couple you just wouldn't like them :-)

漫雪独思 2024-12-10 04:33:04

使用命名空间完全限定类名。您还可以更轻松地在文件之间进行引用,请使用 使用关键字。

 using Lib2Settings = PC.MyCompany.Project

 public class foo {
     public somemethod() {
         var value1 = caLibClient.Settings.MyProperty1  // fully-qualified
         var value2 = Lib2Settings.MyProperty2          // utilizing the using keyword
     } 
 }

Fully qualify the class name with the namespace. You can also make it easier to reference from file to file, use the using keyword.

 using Lib2Settings = PC.MyCompany.Project

 public class foo {
     public somemethod() {
         var value1 = caLibClient.Settings.MyProperty1  // fully-qualified
         var value2 = Lib2Settings.MyProperty2          // utilizing the using keyword
     } 
 }
北笙凉宸 2024-12-10 04:33:04

这取决于尝试使用它们的 C# 文件中的 using 语句。

仅假设:

using caLibServer;

尝试 Settings.MyProperty1 时,它将在 caLibServer 中看到该值。

与“only”类似:

using caLibClient;

Settings.MyProperty1 将仅看到 caLibServer 中的那个。

如果同时拥有两者(无论顺序如何):

using caLibServer;
using caLibClient;

Settings.MyProperty1 将完全失败,您必须指定 caLibServer.Settings.MyProperty1Settings.MyProperty1代码>.

不过,您可以为每个设置设置别名,方法是:

namesoace SomeProjectThatUsesBoth
{
    using Server_Settings = caLibServer.Settings;
    using Client_Settings = caLibClient.Settings;

    // You usage class here
}

然后您可以使用 Server_Settings.MyProperty1Client_Settings.MyProperty1 等别名(假设您将使用以下别名)长度比原始命名空间短,我在这里使用下划线来表明这些不是常用的名称,但您不必这样做唯一的要求是将它们放在命名空间内。定义)。

This depends on the using statements in your C# files that try to use them.

Assuming only:

using caLibServer;

When trying Settings.MyProperty1 it'll see the one in caLibServer.

Similarly with "only":

using caLibClient;

Settings.MyProperty1 will see only the one in caLibServer.

If having both (regardless of order):

using caLibServer;
using caLibClient;

Settings.MyProperty1 will fail completely, you'll have to specify caLibServer.Settings.MyProperty1 or Settings.MyProperty1.

You can set aliases to each of them though, by doing:

namesoace SomeProjectThatUsesBoth
{
    using Server_Settings = caLibServer.Settings;
    using Client_Settings = caLibClient.Settings;

    // You usage class here
}

Then you can use the aliasses like Server_Settings.MyProperty1 or Client_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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文