Dynamically replace the contents of a C# method?

后端 未结 10 2423
面向向阳花
面向向阳花 2020-11-22 16:09

What I want to do is change how a C# method executes when it is called, so that I can write something like this:

[Distributed]
public DTask Solve         


        
10条回答
  •  遥遥无期
    2020-11-22 16:34

    There exists a couple of frameworks that allows you to dynamically change any method at runtime (they use the ICLRProfiling interface mentioned by user152949):

    • Prig: Free and Open Source!
    • Microsoft Fakes: Commercial, included in Visual Studio Premium and Ultimate but not Community and Professional
    • Telerik JustMock: Commercial, a "lite" version is available
    • Typemock Isolator: Commercial

    There are also a few frameworks that mocks around with the internals of .NET, these are likely more fragile, and probably can't change inlined code, but on the other hand they are fully self-contained and does not require you to use a custom launcher.

    • Harmony: MIT licensed. Seems to actually have been used sucessfully in a few game mods, supports both .NET and Mono.
    • Deviare In Process Instrumentation Engine: GPLv3 and Commercial. .NET support currently marked as experimental, but on the other hand has the benefit of being commercially backed.

提交回复
热议问题