是否有 java.lang.instrument 提供的功能的 .Net 模拟?
我研究了一些著名的面向 AOP 的 .Net 框架,例如 PostSharp、bltoolkit、Castle、Cecil 和 Microsoft 的 Policy Injection Block。 也许我很无知,但这些框架似乎不提供在虚拟机加载类时、在应用程序可见之前注入代码的能力。 它们似乎都依赖于应用程序使用工厂或类/方法级属性,这些属性提供程序集编译时操作所需的元数据。 java. lang.instrument 我正在寻找的是简单地在方法调用周围注入拦截器,而无需更改源(方法/类上的属性)或重建现有程序集以注入拦截代码。
I have looked at a few of the well-known AOP-oriented frameworks for .Net such as PostSharp, bltoolkit, Castle, Cecil, and Policy Injection Block from Microsoft. Perhaps I am ignorant, but it appears that these frameworks do not provide the ability to inject code while the class is being loaded by the virtual machine, before it is visible to the application. They all appear to rely on either application use of a factory or class/method level attributes that provide the meta-data needed for compile-time manipulation of assemblies. The critical feature of java.lang.instrument I'm looking for is to simply inject interceptors around method calls without changing source (attributes on methods/classes) or rebuilding existing assemblies to inject the interception code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是正确的,大多数 .NET 的 AOP 框架都要求您使用特殊类型的工厂等来创建对象。 原因是 .NET 中的(自定义)属性是被动的,因此您需要某种框架来及时检查这些属性。
有一些 .NET 的 AOP 框架使用代码生成或 IL 编织(正常编译后修改中间语言字节代码)来允许拦截,但我总是远离类似的事情 - 这有很多固有的问题这样的做法。
我曾经有机会问 Anders Hejlsberg 我们是否无法在 .NET 中获得“主动”属性,但他的回答是,如果微软提供了这样的功能,他们可能会停止该平台的所有进一步开发,因为这会不可能在不破坏与某人代码的兼容性的情况下推动 .NET 向前发展(我可能歪曲了他的答案,但重要的是它已被考虑并拒绝 - 显然有充分的理由)。
也就是说,.NET 有一个可以拦截任何方法调用的 Instrumentation API。 但是,该 API 是非托管 API(它要求您编写非托管 C++ 代码)并且 (IIRC) 要求您自己托管 .NET 应用程序。 这是一种非常严厉的做法。 您还应该意识到 Instrumentation 可能会损害您的应用程序的性能。
You are correct that most of the AOP frameworks for .NET requires you to create objects using a special kind of factory or the like. The reason for this is that (custom) attributes in .NET are passive, so you need some kind of framework that can inspect those attributes in a timely fashion.
There are a few AOP frameworks for .NET that use either code generation or IL weaving (modification of the Intermediate Language byte code after normal compilation) to allow interception, but I always stay away from things like that - there's a lot of inherent problems in such an approach.
I once had an opportunity to ask Anders Hejlsberg whether we couldn't get 'active' attributes in .NET, but his response was that if Microsoft provided such a capability, they might as well stop all further development of the platform, because it would be impossible to move .NET forward without breaking compatibility with someone's code (I may be misrepresenting his answer, but the essential part is that it has been considered and rejected - apparently for good reasons).
That said, .NET has an Instrumentation API that can intercept any method call. That API, however, is an unmanaged API (it requires you to write unmanaged C++ code) and (IIRC) requires you to host the .NET app yourself. It is a very heavy-handed approach. You should also be aware that Instrumentation can potentially harm the performance of your application.
您看过这篇文章吗:比较 IoC 框架?
Have you seen this write-up:comparing IoC frameworks?