重写.Net DLL的方法 [英] Rewrite a method of a .Net DLL

查看:255
本文介绍了重写.Net DLL的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧的.NET DLL,我将源代码丢失了,我想看看是否可以更改其行为。



它有一个接收字符串并返回字符串的方法。



我想重写一下这样我就可以返回

  startStr + test的方法; 

我尝试了.NET反射器,但是它产生的项目有很多奇怪的错误,所以我无法重新编译它。



然后我尝试了Reflexil,但它只提供了装配级别的更改。



有什么办法可以

解决方案

Reflexil应该能够为您处理这个问题-您需要在Reflector中切换到IL视图,然后可以转到要更改的方法,拉起Reflexil并进行更改。这样可以避免将程序集反编译为源代码的问题(对于我来说,在Reflector中没有错误,这对我没有用)。



如果您要做的就是将字符串附加到字符串变量,您可以执行以下操作:

  //假设您的原始字符串已经在堆栈中
ldstr 测试
调用System.String System.String :: Concat(System.String,System.String)

这将在堆栈上创建一个新字符串,并在其后附加 test 。完成编辑后,您可以将程序集保存回磁盘。



如果您需要更复杂的操作(例如附加函数调用返回的字符串) ,您只需要调用正确的方法,然后在堆栈上的两个字符串上调用 Concat()



如果您的原始方法返回一个字符串,那么将其包装在新程序集中的新函数中可能是一个更好的解决方案。仅在确实需要更改原始程序集的情况下,才编辑IL,因为-例如,从特定方法返回的字符串用于同一程序集,并且您需要该程序集中的其他函数来查看更改后的返回值。 / p>

注意:为此,我使用了Reflector 6.8.2.5和Reflexil 1.0-当前的Reflector / Reflexil可能有所不同。让我知道是否需要这些文件进行更改。


I have an old .NET DLL which I have lost the source code to and I wanted to see if I could change its behavior.

It has a method that takes in a string and returns a string.

I would want to rewrite this method such that I can return

startStr + " test";

I tried .NET reflector but the project it produced has a bunch of strange errors so I cannot recompile it.

I then tried Reflexil, but it only offers assembly level changes.

Is there any way I could rewrite the method in C# and have the DLL use my method instead?

解决方案

Reflexil should be able to handle this for you - you need to switch to IL view in Reflector and then you can go to the method that you want to change, pull up Reflexil and make your changes. This avoids the problems with decompiling the assembly to source code (which never worked for me without errors in Reflector).

If all you want to do is append a string to a string variable, you can just do something like:

// assuming your original string is already on the stack
ldstr " test"
call System.String System.String::Concat ( System.String, System.String )

This will create a new string on the stack with test appended to it. Once you're done with the editing, you can save the assembly back to disk.

If you need something more complicated (like appending a string returned by a function call), you simply need to call the right method and then call Concat() on the two strings on the stack.

If your original method returns a string then wrapping it in a new function in a new assembly may be a better solution, though. I'd only edit the IL if you really need the original assembly to change because - for example - the string returned from the particular method is used within that same assembly and you need other functions in that assembly to see the changed return value.

Note: I used Reflector 6.8.2.5 and Reflexil 1.0 for this - current Reflector / Reflexil may be different. Let me know if you need these files for your changes.

这篇关于重写.Net DLL的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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