-1

I have a shared Project.Core class library and there are

public class SomeClass
{
    //properties
    //methods
}

public class AnotherClass
{
    //properties
    //methods
}

then I have an web app (Project.Web) and desktop app (Project.Client) that refer the project, there I want to add some extension to SomeClass so

public class SomeClass : Project.Core.SomeClass 
{
    //Extensions
}

But it cause an ambiguousness between Project.Web.SomeClass and Project.Core.SomeClass when there's a file that have

using Project.Core //to refer uninherited classes in the class library
using Project.Web  //to refer inherited Project.Web.SomeClass

public class MyClass
{
    public SomeClass SomeClass { get; set; } //Ambiguousness
    public AnotherClass AnotherClass { get; set; }
}

So I have two ways: change the name in the class library to something like SomeClassCore or change inherited classes to SomeClassWeb and SomeClassClient.

Which is the better choice, or is there another smart way?

kemakino
  • 1,041
  • 13
  • 33
  • It's really just a matter of opinion. But _my_ opinion is that it makes no sense to reuse the class name. `SomeClass` is presumably a `Project.Core.SomeClass`, plus some stuff. Its name ought to reflect that "some stuff" that's been added. If you want to change the base class name instead, that's fine too. But I think it would be more typical to change the names of the derived classes (see, e.g. `Stream`, vs `FileStream`, `MemoryStream`, etc.) – Peter Duniho Feb 08 '18 at 07:34
  • Another option in those classes with both using statements is to use the fully qualified name when you're instantiating it: `Project.Core.SomeClass pcSomeClass = new Project.Core.SomeClass()` – mcalex Feb 08 '18 at 08:50

1 Answers1

4

You can use alias for namespaces. Simply define some alias to the namespaces which you are going to import and refer alias where naming are creating ambiguity

In your case instead of changing name of class you can alias namespaces. Here is the code:

using web = Project.Web
using core = Project.Core

when you want to refer SomeClass from both the projects then you can write something like this

web.SomeClass        //This will refer class from Project.Web
core.SomeClass       //This will refer class from Project.Core

Using Directive Here you can find more about using directive and aliasing the references

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44