简体   繁体   English

如何重定向方法调用?

[英]How to redirect a method call?

I have got a DLL which works fine in Windows, but Inside one of its private functions the static System.IO.File.ReadAllBytes() is called. 我有一个在Windows上可以正常运行的DLL,但是在其私有功能之一中,调用了静态System.IO.File.ReadAllBytes()。 I have to use this DLL on a windows CE smart Device Project with Compact Framework 3.5 and there is no such method in the System.IO.File. 我必须在带有Compact Framework 3.5的Windows CE智能设备项目上使用此DLL,并且System.IO.File中没有此类方法。 I have tried to create a class named "File" inside the project like this: 我试图在项目内部创建一个名为“ File”的类,如下所示:

public class File
{
     internal static byte[] ReadAllBytes(string path)
     {
     }

     internal static void WriteAllBytes(string path, byte[] bytes)
     {
     }
}

My own calls to the static Methods of the class File are redirected here But the calls inside the DLL methods still go to the System.Io.File class and I still get the MissingMethodException. 我自己对File类的静态方法的调用在此处重定向,但是DLL方法内部的调用仍然转到System.Io.File类,并且我仍然得到MissingMethodException。 I tried the methods with public modifiers but saw no change. 我尝试使用公共修饰符的方法,但没有发现任何变化。

I even tried to rewrite the public method that calls the private method inside which the ReadAllbytes was invoked and used MethodInfo.Invoke with no success. 我什至尝试重写公用方法,该公用方法调用在其中调用ReadAllbytes并使用MethodInfo.Invoke的私有方法,但未成功。

The question: Is there a way to force the method inside the Dll to accept my ReadAllbytes Method instead of System.File.IO.ReadAllBytes()? 问题:是否有办法强制Dll中的方法接受我的ReadAllbytes方法而不是System.File.IO.ReadAllBytes()? The invocation inside the DLL is like this: DLL中的调用如下:

using System.IO.File;

namespace Something
{
    class SomeClass
    {
        public Boolean someMethod()
        {
            byte[] myBytes = File.ReadAllBytes(filePath);
        }
     }
}

Static methods like File.ReadAllBytes are resolved at compile time to a specific Assembly name and class. 诸如File.ReadAllBytes的静态方法在编译时解析为特定的Assembly名称和类。 You will need to modify the DLL to change the call. 您将需要修改DLL才能更改调用。 Prehaps you could decompile and recompile it, or edit the IL. 可能您可以对其进行反编译和重新编译,或者编辑IL。

*It is possible to redirect the call using the profiling hooks (the sameone the debugger uses), and that is how the Moles Mocking framework works. *可以使用分析挂钩(调试器使用的挂钩)重定向调用,这就是Moles Mocking框架的工作方式。 However it would not be suitable for production use. 但是,它不适合生产使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM