Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two identical classes in separated libraries and in one project

Tags:

c#

class

partial

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
        }
    }
like image 567
Tomas Avatar asked Dec 06 '25 01:12

Tomas


2 Answers

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.

like image 75
Daniel Hilgarth Avatar answered Dec 07 '25 13:12

Daniel Hilgarth


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.

like image 45
masimplo Avatar answered Dec 07 '25 15:12

masimplo