Home > database >  How to attach VS breakpoints to dynamically compiled code?
How to attach VS breakpoints to dynamically compiled code?

Time:02-03

I have some code compiled dynamically and am trying to understand how to get breakpoints working for it

First off, I can trigger a breakpoint with System.Diagnostics.Debugger.Break, and looking at module viewer, the PDB is found and symbols are loaded. However, I don't know how to get a breakpoint that I set via editor to trigger - looking through resources online I could not figure out where exactly are breakpoints stored

Debugging the code results in entrypoint bin/Debug/Addons.dll and bin/Debug/Addons.pdb being generated, and bin/Debug/AddonsOut is populated with *.cs, *.cmdline *.err *.out *.tmp file. Everything runs successfully, printing the expected output, but no breakpoint

Entrypoint

using Interface1;
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            CSharpCodeProvider compiler = new CSharpCodeProvider(new Dictionary<String, String> { { "CompilerVersion", "v4.0" } });
            var parms = new CompilerParameters();
            parms.GenerateExecutable = false;
            parms.CompilerOptions = "/unsafe";
            parms.GenerateInMemory = false;
            parms.IncludeDebugInformation = true;
            parms.ReferencedAssemblies.Add("System.dll");
            parms.ReferencedAssemblies.Add("Interface1.dll");
     
            DirectoryInfo dirTemp = new DirectoryInfo(Path.Combine("./", "AddonOut"));

            if (!dirTemp.Exists)
            {
                Directory.CreateDirectory(dirTemp.FullName);
            } else
            {
                try
                {
                    foreach (FileInfo f in dirTemp.GetFiles())
                    {
                        f.Delete();
                    }
                }
                catch (Exception) { }
            }

            parms.TempFiles = new TempFileCollection(dirTemp.FullName, true);
            parms.OutputAssembly = Path.Combine("./", "Addons.dll");

            string[] files = Directory.GetFiles(Path.Combine("../../", "Addons"));
            Dictionary<string, string> scripts = new Dictionary<string, string>();
            foreach (var file in files)
            {
                var fileName = new FileInfo(file).Name;
                scripts[fileName] = File.ReadAllText(file);
                Console.WriteLine(fileName);
            }

            CompilerResults results = compiler.CompileAssemblyFromSource(parms, new string[] { scripts["Class1.cs"] });
            foreach (var type in results.CompiledAssembly.GetTypes())
            {
                Console.WriteLine(type.FullName);
                Console.WriteLine(typeof(IInterface1).IsAssignableFrom(type));
                var res = (IInterface1)results.CompiledAssembly.CreateInstance(type.FullName);
                res.InitMe();
            }

            Console.ReadLine();
        }
    }
}

In entrypoint folder /Addons

using Interface1;
using System;

namespace ConsoleApp1.Addons
{
    public class Class1 : IInterface1
    {
        public void InitMe()
        {
            Console.WriteLine("Hello!"); // checked as debug
            Console.WriteLine("World!"); // checked as debug
        }
    }
}

and the separate ClassLibrary

namespace Interface1
{
    public interface IInterface1
    {
        void InitMe();
    }
}

CodePudding user response:

So, quite saddening, but I resolved my own issue without figuring out what was causing it. I fiddled around and replaced

CompilerResults results = compiler.CompileAssemblyFromSource(parms, files[0]);

for

CompilerResults results = compiler.CompileAssemblyFromFile(parms, /* path to the same file */);

Now the breakpoints are triggering as expected!

  •  Tags:  
  • Related