且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

编程方法添加到类?

更新时间:2022-03-16 07:45:28

实际上,有没有办法,你要修改现有的类在你想要的方式。即使使用Reflection.Emit的,它不会做你想做的,因为API调用是一个特定的类型,您可以在运行过程中不能修改一个类型的定义。您可对Reflection.Emit的做的就是定义一个类继承自代理,但得到的API来加载你的继承类型可能会证明,除非他们正在使用依赖注入框架,这似乎不太可能是不可能的。

Realistically, there's no way you're going to modify an existing class in the way you want. Even using Reflection.Emit, it won't do what you want because the API is calling a specific type, and you can't modify the definition of a type during runtime. What you can do with Reflection.Emit is define a type that inherits from the proxy, but getting the API to load your inherited type will probably prove to be impossible unless they're using a dependency injection framework, which seems unlikely.

唯一真正的方式做,这是去中和使用反编译和重新编译修改DLL的字节code。这是Postsharp怎么做AOP,但海事组织,我从来没有做任何其他原因。

The only real way to do this is to go in and modify the bytecode of the dll using decompilation and recompilation. This is how Postsharp does AOP, but imo, I'd never do this for any other reason.

如果您有任何访问该code,甚至他们的图书馆,那么有可能是在你的问题的一个更好的办法。

If you have any access to this code or even their libraries, then there is probably a better way around your problem.