user5405648
user5405648

Reputation:

Can't access file from seperate assembly?

I have two projects in my solution. One represents the main project and another one has the name of the first project but adds ".API" at the end, which acts as an assembly for my interfaces.

Since I use an interface for specific classes, I access the interface and not the actual concrete class, this brings a problem when I was to access a file from the main assembly inside a file in the main assembly and that's fine, the problem comes as I need to mention it in the interface file.

Otherwise it wouldn't be accessible as the interface file is our class in this example.

Here is a code example...

namespace App.Classes
{
    public class User : IUser 
    {
        public SomeType SomeType { get; set; }
    }
}

namespace App.Classes
{
    public enum SomeType
    {
        SpecialType,
        GoldType,
        SilverType,
        Other
    }
}

namespace App.API
{
    public interface IUser
    {
        public SomeType SomeType { get; set; }
    }
}

The error I am receiving is the type or namespace name 'SomeType' could not be found.

When trying to add using App to the interface file I receive an error telling me that namespace doesn't exist.

Primary assembly name: App
API assembly name: App.API

Upvotes: 3

Views: 93

Answers (1)

TheGeneral
TheGeneral

Reputation: 81593

If i understand you correctly,

You have referenced your API (App.API), from you main app (App).

You are then trying to call/reference SomeType in your API which actually located back in (App).

Basically this (if it could be done) is called a Circular Reference for which .Net disallows.

Further more

The type or namespace name 'SomeType' could not be found

This error is entirely appropriate, because there is no reference (even check your API project) from the App.API project to App. I know its not there because it cant be done, .Net wont let you. Ergo Circular Reference

You need to make common things common, i.e If your API needs to know about SomeType it has to be placed in your API assembly (or a more common assembly that both App and App.API can reference).

The simple solution is to put SomeType back into App.API (the project, not just the namespace)

namespace App.API.Enums
{
    public enum SomeType
    {
        SpecialType,
        GoldType,
        SilverType,
        Other
    }
}

Or to create a 3rd assembly and reference it from both the App and App.Api projects

Upvotes: 2

Related Questions