Project a Can't Reference a Class in Project B

Project A can't reference a class in Project B

When you add a reference in your XAML, try to Rebuild your project once to recompile everything and to incorporate the referenced project and to make any new controls available. It is necessary when a file or a dependency has been changed, or even with no reason. But it will solve your issue.

Why can't I reference a class in another namespace in another project within the same solution?

Since the type is public, this sounds as simple as a missing project reference between the two. Right click references and add a new reference, but watch that references shouldn't become circular (i.e. A <===> B, or A ===> B ===> C ===> A). Actually, the VS IDE doesn't let you do this, but it can be done (accidentally or purposefully) via the command-line tools.

Project reference is enabling me to use code from other dependent projects without explicit project reference

If you reference a project that has references to another project, those references will be automatically added. In your case when you have Project A with a reference to Project B, when you reference project A in project C reference to Project B will be automatically added.

If you would like to disable transitive reference behavior you can add PrivateAssets="All" to your reference in the ProjectA.csproj (WebProject)

<ItemGroup>
<ProjectReference Include="..\ClassLibrary1\ClassLibraryProject.csproj" PrivateAssets="All"/>
</ItemGroup>

Sample Image

Why can't I reference my class library?

I found how to fix this issue (for me at least). Why it worked, I'm not sure, but it did. (I just tried against a second website that was having the same problem and the following solution worked for that as well).

I tried the normal cleaning of the projects and rebuilding, shutting down all my Visual Studio instances and restarting them, even tried restarting my computer.

What actually worked was opening up the project in Visual Studio, closing all the open tabs, and then shutting it down.

Before I had left the tabs open because I didn't think it mattered (and I hardly ever close the tabs I'm using).

Referencing a project in another project creates unwanted dependencies

You probably need a shared dll.

You have created utility classes in project A out because they are shared all across project A (Application A?), now you have introduced project B (Application B) and as you state it needs to get hold of the code from projectA.dll/exe.

So create a new project in your solution (Ab.Shared.dll maybe:-)) and move your utiilty classes into it. You can now reference that dll from both project A and project B.

Update: Just read about your comment about sucking code out.

The shared dll is the most common way of sharing the code about, but there are other ways. Theoretically you can simply "include" the same *.cs files in both projects and share them that way (use the drop down on the Add existing item dialog and select Add as link) . However in practice it becomes more awkward maintaining this scenario so most people use a shared dll.

How to expose a class to an external Project without Reference in c#

Solution Idea

I would use an Interface Layer for this (called Project I).

This Project I contains the interface ICar.

The other Projects A, B and C reference to I.

Your Car class should implement this interface and look something like this:

public class Car : ICar
{
// ... some great Code
}

In Project B your function would be:

public List<ICar> GetCars()
{
var myCars = new List<Car>();

// ... some really special magic

return (List<ICar>)myCars;
}

Now your code, in project C can use this function like this:

List<ICar> cars = B.GetCars();

Why using an Interface Layer?

Because then you will gain more maneuverability in your code. This is great for using Dependency Injection or Unit Tests. It will help you replace parts of your implementation in certain situations for example providing Mock objects during Unit Testing.


Design Approach

To prevent spaghetti code i would recommend to look up Jeffery Palermo's concept of Onion Architecture.

There are some great Question about it on SO:

  • What are the typical layers in an onion architecture?

  • best layer for placing dependency injection related code in layered proeject

  • Onion Architecture, Unit of Work and a generic Repository pattern

How to use a class from one C# project with another C# project

Simply add reference to P1 from P2

Java Netbeans circular dependency in included project

To answer the only question you actually asked, yes you are doing something wrong and this is not a Java/classpath limitation.

B is trying to extend classes from A, and A is also trying to load classes from B. This cannot be done.

You need to either

  1. Combine A and B into one project
  2. Remove the shared code from one of the projects (e.g. the classes from A that B needs) and put them in another project, C, on which both A and B depend.


Related Topics



Leave a reply



Submit