有没有办法使调试器与修改的组件一起工作

发布于 2025-02-07 17:51:07 字数 1949 浏览 3 评论 0原文

在使用之前,我正在尝试修改组件。
主文件:

using IlGenTestTarget;
using Lokad.ILPack;
using System.Reflection;
using Mono.Cecil;
using IlGenTest;

Assembly inAssembly = Assembly.GetAssembly(typeof(Class1));
AssemblyGenerator assemblyGenerator = new AssemblyGenerator();
byte[] b = assemblyGenerator.GenerateAssemblyBytes(inAssembly);
AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly(new MemoryStream(b));
foreach (ModuleDefinition module in assemblyDefinition.Modules) {
    IlGenTestUtils.RemoveRemovedElements(module.Types);
    foreach (TypeDefinition type in module.Types) {
        IlGenTestUtils.RemoveRemovedElements(type.Methods);
        IlGenTestUtils.RemoveRemovedElements(type.Fields);
    }
}
MemoryStream ms = new MemoryStream();
assemblyDefinition.Write(ms);
byte[] res = ms.ToArray();
Assembly resAssembly = Assembly.Load(res);
Module resModule = resAssembly.GetModules()[0];
Type resType = resModule.GetType("IlGenTestTarget.Class1");
MethodInfo resMethod = resType.GetMethod("Method1");
resMethod.Invoke(null, null);

iLgentEstutils:

using Mono.Cecil;
using Mono.Collections.Generic;

namespace IlGenTest
{
    public class IlGenTestUtils
    {
        public static List<T> GetRemovedElements<T>(Collection<T> collection) where T : ICustomAttributeProvider
        {
            return collection
                .Where(t => t.CustomAttributes.Any(attr => attr.AttributeType.Name == "RemovedAttribute"))
                .ToList();
        }

        public static void RemoveRemovedElements<T>(Collection<T> collection) where T : ICustomAttributeProvider
        {
            foreach (T t in GetRemovedElements<T>(collection))
            {
                collection.Remove(t);
            }
        }
    }
}

当我在Method1上放断点时,一切都很好,但是Progam并未在其上停下来。当我直接调用Method1时,而无需创建新的组件,程序会如预期的那样在断点上暂停。有没有办法使断点与动态组装一起使用?

I am trying to modify assembly before using it.
Main file:

using IlGenTestTarget;
using Lokad.ILPack;
using System.Reflection;
using Mono.Cecil;
using IlGenTest;

Assembly inAssembly = Assembly.GetAssembly(typeof(Class1));
AssemblyGenerator assemblyGenerator = new AssemblyGenerator();
byte[] b = assemblyGenerator.GenerateAssemblyBytes(inAssembly);
AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly(new MemoryStream(b));
foreach (ModuleDefinition module in assemblyDefinition.Modules) {
    IlGenTestUtils.RemoveRemovedElements(module.Types);
    foreach (TypeDefinition type in module.Types) {
        IlGenTestUtils.RemoveRemovedElements(type.Methods);
        IlGenTestUtils.RemoveRemovedElements(type.Fields);
    }
}
MemoryStream ms = new MemoryStream();
assemblyDefinition.Write(ms);
byte[] res = ms.ToArray();
Assembly resAssembly = Assembly.Load(res);
Module resModule = resAssembly.GetModules()[0];
Type resType = resModule.GetType("IlGenTestTarget.Class1");
MethodInfo resMethod = resType.GetMethod("Method1");
resMethod.Invoke(null, null);

IlGenTestUtils:

using Mono.Cecil;
using Mono.Collections.Generic;

namespace IlGenTest
{
    public class IlGenTestUtils
    {
        public static List<T> GetRemovedElements<T>(Collection<T> collection) where T : ICustomAttributeProvider
        {
            return collection
                .Where(t => t.CustomAttributes.Any(attr => attr.AttributeType.Name == "RemovedAttribute"))
                .ToList();
        }

        public static void RemoveRemovedElements<T>(Collection<T> collection) where T : ICustomAttributeProvider
        {
            foreach (T t in GetRemovedElements<T>(collection))
            {
                collection.Remove(t);
            }
        }
    }
}

When I put breakpoint on Method1, everything works fine, but progam is not paused on it. When I invoke Method1 directly, without creating new assembly, program is paused on breakpoint as expected. Is there a way to make breakpoints work with dynamic assembly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛起拖把扫天下 2025-02-14 17:51:07

“放置断点的源线”和“汇编文件内容”之间的链接由.pdb文件定义。修改组件后,如果它们之间的链接仍然有效,我实际上会感到惊讶。

您还需要重建.pdb文件,如果不是不可能,这似乎很难。

The link between "source line at which a breakpoint is placed" and "assembly file contents" is defined by the .pdb file for the assembly. After modifying the assembly, I would actually be surprised if the link between them would still work.

You would need to also rebuild the .pdb file, which seems hard if not impossible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文