Michel Feinstein
Michel Feinstein

Reputation: 14266

Conflicting Namespace Resolution

I am using a library called BCrypt.net, the author made the Namespace the same name as the Class, so the Class full path is: BCrypt.Net.BCrypt, where BCrypt.Net is the Namespace and BCrypt is the Class name.

I am trying to use in my code, like all the examples I can find use, like:

BCrypt.HashPassword("234");

But Visual Studio complains saying:

Error 3 The type or namespace name 'HashPassword' does not exist in the namespace 'BCrypt' (are you missing an assembly reference?)

I have the assembly in my project (since I got it from NuGet):

Assembly Reference

If I add a using BCrypt.Net; or using BCrypt;, before my namespace the error says the same. I I add it inside my namespace, something funny happens. I works, the code compiles and executes. BUT Visual Studio shows an error! I can't understand how it compiles with an error.

namespace Test.Data
{
    using BCrypt.Net; // The 'Net' is marked with a Red Error line in VS2013
    ....
    string s = BCrypt.HashPassword("234");

Error 3 The type name 'Net' does not exist in the type >'BCrypt.Net.BCrypt'

The error (but compiles and executes fine) is the same for using BCrypt = BCrypt.Net.BCrypt;

So what's going on?


Edit:

I know I can use it as BCrypt.Net.BCrypt.HashPassword("234");, but I want to avoid it.

Upvotes: 1

Views: 2397

Answers (2)

Alexander Schmidt
Alexander Schmidt

Reputation: 5723

Add

using BCrypt = BCrypt.Net.BCrypt;

to your usings:

namespace YourNamespace
{
    using BCrypt = BCrypt.Net.BCrypt;

    class Program
    {
        static void Main(string[] args)
        {
            BCrypt.HashPassword("234");
        }
    }

Proofing screen:

VS 2015 likes this

Upvotes: 2

MAC
MAC

Reputation: 151

I think you can solve this issue adding an alias to the namespace. Something like this:

using BCr = BCrypt.Net;

namespace Program {

   public class MyClass {

     public void MyMethod() {
        var s = BCr.BCrypt.HashPassword("234");
     }
  }
}

Upvotes: 3

Related Questions