Search code examples
c#visual-studio.net-4.8

VS 2022 keep refering to old code of class library even after changing code


I have a solution with multiple projects inside. The first is a class library, the others are the console application and a unit test project. The problem is the first time I ran the console app or unit test project, everything seemed okay, but after several times, any changes I made to the class library were not updated to other projects. When I debug in, the code keeps referring to the old code of the class library instead of the new one. For example, my class library has code like this

public void PrintLine()
{
  Console.WriteLine("Old code");
}

After I updated it to this

public void PrintLine()
{
  Console.WriteLine("New code");
}

After cleaning and rebuilding the solution, and re-add references for other projects, it still prints out "Old Code"

Thing I've tried

  • Clean, rebuild solution
  • Re-add reference (sometimes it works, sometimes it does not)
  • Restart vs 2022 and window
  • Delete bin/debug folder in projects
  • All projects have the same .net framework version
  • Build projects one by one to make sure none of them have any build error

I'm not sure why that happens. I try creating a new project and this thing happens again just after a few runs

Update

After struggling for 1 night, I found that in the modules call, the DLL of the class library I'm referring to is in a different path instead of the bin/debug I've set. This dll file was created 2 days ago, I don't know how and when but after deleting it, thing seems working again

C:\Windows\Microsoft.NET\assembly\GAC_MSIL\(Class library name)

enter image description here


Solution

  • You can find the dll with the corresponding name in the C:\Windows\Microsoft.NET\assembly\GAC_MSIL path and delete it.

    GAC_MSIL is a folder in the Global Assembly Cache. The Global Assembly Cache is a special location used to store and manage shared .NET assemblies. In the .NET Framework, an assembly is a unit of code base that can contain a set of related classes, interfaces, and other types of definitions.

    Assemblies in the GAC have a certain priority because they are globally shared. When multiple applications require the same version of an assembly, the assembly will be called first to ensure that they are using the same assembly.