Debugging a managed DLL

Hello!

I use the following products:

  • Unity 2020.1.2f1
  • Visual Studio 2019 16.7.2
  • Visual Studio 2019 Tools for Unity 4.7.1.0

In Visual Studio, I create a blank solution and call it Base. Then I add a class library project and call it Base. For the project, I install .NET Standard 2.0 as the target platform. Debugging information is automatically set to Portable. I add class MyUtils to the project:

namespace Base
{
    public static class MyUtils
    {
        public static int add(int a,int b)
        {
            return a+b;
        }
    }
}

Then I build the project and get three files:

  • Base.dll
  • Base.pdb
  • Base.deps.json

Now I create a new Unity project and call it Test. I copy two files to the root of the project (to the Assets folder on the disk):

  • Base.dll
  • Base.pdb

Although I didn’t notice any difference from having the pdb file in the project. In the project settings I have installed:

However, the library itself says:

And this is a little scary since I installed .NET Standard 2.0. I add script Test to the project:

using UnityEngine;

public sealed class Test : MonoBehaviour
{
    void Start()
    {
        int a=1;
        int b=2;
        int c=Base.MyUtils.add(a,b);

        Debug.Log(c);
    }
}

I place this script on the camera. I put a breakpoint on the line with the called function and start debugging from Visual Studio:

Switch Unity to debug mode and play. My breakpoint is triggered:

But after pressing Step Into (F11), I don’t go inside the function:

What actions do I need to do so that I can debug the code by going inside functions?

I was reading this documentation (section “Debug a DLL in your Unity project”). But something I still did not understand in which project where they add what and why the project file is added to the project in the screenshot, and the link that they provide tells about adding the project to the solution. So I tried it and it didn’t work.

If the DLL is, in fact, targeting NET4* and your Unity project is targeting NETSTANDARD2.0, you will likely not be able to step into the DLL, even with the PDB file.

If the listing of NET4* is incorrect, make sure that the build of the DLL you are using was done in Debug. PDBs can be generated even with Release builds in the event that you want to be able to see calls to symbols from the library, but if it’s built in Release, it also won’t be available to step into.

Here is the content of the Base project file:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <RootNamespace>Base</RootNamespace>
    </PropertyGroup>

</Project>

You can see that .NET Standard 2.0 is indicated. The assembly is done by Debug. Unity had .NET Standard 2.0 installed at the Api Compatibility Level. It’s strange why Targets .NET 4.x is written on the library itself (when looking in Unity) in the Assembly info field. But I tried to switch the Unity project to .NET 4.x, debugging also skips the function. What to do?