Search code examples
c#enumsreferencesolution

Enum cannot work when reference it cross multi visual studio solution


I have three solution A, B and C. I have an enum in A like this:

using System.Runtime.Serialization;
namespace A.Entities
{
    [DataContract]
    public enum Status
    {
        [DataMember]
        Active = 0,

        [DataMember]
        Inactive = 1,
    }
}

I reference it in solution B like this:

using A.Entities;
namespace B.Entities
{
    public class User
    {
        [DataMember]
        public string UserName { get; set; }

        [DataMember]
        public Status Status { get; set; }
    }
}

In solution C, I use them like this:

using B.Entities;
using Status = A.Entities.Status;
namespace C.TestDatas
{
    public class UserTestData
    {
        public static User CreateUser()
        {
            return new User
                   {
                       Status = Status.Active,
                   }
        };
    }
}

When I invoke User method, it throw exception:

Method not found: 'Void B.Entities.User.set_Status(A.Entities.Status)'.

Why? I hope someone can help me, thanks!


Solution

  • I can't reproduce the problem. Your code works fine on my computer (on .NET framework 4.5)

    Could you make sure that you project is configured correctly:

    1. Both A and B solutions have reference to System.Runtime.Serialization
    2. C has reference to A and B. Make sure you don't link the dll from /Bin folder, but you have the project reference
    3. Hit Clean and Rebuild buttons in Visual Studio.