有没有一种方式挂接在C#中的管理功能,就像我会一个非托管函数的C ++? [英] Is there a way to hook a managed function in C# like I would a unmanaged function in C++?

查看:125
本文介绍了有没有一种方式挂接在C#中的管理功能,就像我会一个非托管函数的C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中我会得到函数的地址并覆盖前几个字节到JMP到我的功能,做一些东西,恢复原始字节,然后调用原有的功能。

In C++ I would get the address of the function and overwrite the first few bytes to a jmp to my function, do some stuff, restore the original bytes, and call the original function.

我可以做这样的事情在C#?

Can I do something like this in C#?

推荐答案

在.NET探查器API是拦截方法在运行时的最接近的微软认证的方式。正如前面提到的,这是有点难以实现,我不知道一个库,使这个容易实现用纯粹的管理code。

The .NET Profiler API is the closest "Microsoft-approved" way of intercepting methods at runtime. As already mentioned, this is somewhat difficult to implement and I am not aware of a library that makes this easy to implement using purely managed code.

在几个月前研究选择此我自己,我偶然在 CLR方法注入,这与源$ C ​​$ C,说明如何截获方法在运行时的文章。我认为用这个自己,甚至有示例项目的工作,但最终的结论是,我需要更多的比的方法拦截,将需要不同的解决方案。然而,这种方法直接回答你的问题。

While researching options for this myself a few months ago, I stumbled on CLR Method Injection, which is an article with source code explaining how to intercept methods at runtime. I considered using this myself, and even had the sample project working, but ultimately concluded that I needed more than method interception and would need a different solution. However, this approach directly answers your question.

最后,我落得这样做发展再思考作为一个开源替代品的 PostSharp ,这作为后编译步骤运行,您可以修改现有的方法来调用其它code或更换方法实现完全。我的工作,现在一个新的流体语法,我将包括现在的样本,以提供一个例子,以帮助你看到这个AOP技术将如何满足您的需求:

Lastly, what I ended up doing was developing Afterthought as an open source alternative to PostSharp, which runs as a post-compile step and allows you to modify existing methods to call other code, or replace the method implementations altogether. I am working right now on a new fluid syntax, which I will include a sample of now to provide an example to help you see how this AOP approach would meet your needs:

Methods
    .Named("Sum")
    .WithParams<int[]>()
    .Before((T instance, ref int[] values)
        => { var s = new Stopwatch(); s.Start(); return s; })
    .After((instance, stopwatch, values)
        => instance.Result = (int)stopwatch.ElapsedMilliseconds);   

在这种情况下,同时修改现有类型与总和方法,我引入code前后测量执行时间的方法之后。对原编译的类再思考运行后,产生的数总和法将不得不调用这些拉姆达EX pressions(C#编译器只是把它们转化成静态方法),在方法体内后。我可以很容易地被称为实施并更换了整个方法的实现。

In this case while amending an existing type with a Sum method I am introducing code before and after the method to measure the execution time. After running Afterthought against the original compiled class, the resulting Sum method would have calls to these lambda expressions (the C# compiler just turns them into static methods) before and after the method body. I could have just as easily called Implement and replaced the entire method implementation.

我希望这些方法中的一种满足您的需求。以我为例,我去了AOP的路线,因为我想要做多拦截,比如实现接口,添加新的方法/属性,等我也想要的东西,不会引入在运行时依赖或担忧在稳定性和性能运行环境 - 编译时的处理仅仅是更安全,更易于测试

I hope one of these approaches meets your needs. In my case, I went the AOP route because I wanted to do more than interception, such as implementing interfaces, adding new methods/properties, etc. I also wanted something that would not introduce dependencies at runtime or concerns about stability or performance in the runtime environment--compile-time processing is just safer and easier to test.

这篇关于有没有一种方式挂接在C#中的管理功能,就像我会一个非托管函数的C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆