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
//ski开发者_Go百科p code
}
}
Lib2.dll
namespace caLibClient
{
public partial class Settings
{
public static string MyProperty2
//skip code
}
}
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.
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.
Try using the same namespace. You can change the default namespace for one of your libraries.
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 :-)
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
}
}
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).
精彩评论