[英]How can I keep a ref to an object's state at a given time?
My goal is to save in a file the callstack state when an exception is raised.我的目标是在引发异常时将调用堆栈状态保存在文件中。 But I have one major restraint: I cannot touch any of the existing code.
但我有一个主要的限制:我不能触及任何现有的代码。 My goal is to implement that in a context where I'm unaware of the current workflow.
我的目标是在我不知道当前工作流程的上下文中实现它。 So I can use it in any C# Project.
所以我可以在任何 C# 项目中使用它。
To do so I need to have access to all the nested methods that lead to the exception but also all the method's arguments.为此,我需要访问所有导致异常的嵌套方法以及所有方法的参数。
Until now I used postsharp OnMethodBoundaryAspect which allow me to define OnEntry, OnSucess, and OnException method that is called whenever a method: is entered, returns, or raise an exception.到目前为止,我使用了 postsharp OnMethodBoundaryAspect,它允许我定义 OnEntry、OnSucess 和 OnException 方法,每当方法:输入、返回或引发异常时都会调用这些方法。 What is great is that I can apply this aspect with a
postsharp.config
file in the project I want.很棒的是,我可以在我想要的项目中使用
postsharp.config
文件应用这个方面。 No source modification, I'm only adding my project to the solution, a config file, and reference to my project in every project of the solution and I'm good to go.没有源代码修改,我只是将我的项目添加到解决方案中,一个配置文件,并在解决方案的每个项目中引用我的项目,我很高兴。 Using that I could maintain a callstack of my own, and I could save references to methods' name, and ref to methods' arguments.
使用它,我可以维护自己的调用堆栈,并且可以保存对方法名称的引用,以及对方法参数的引用。
Now let's say a methodA
calls a methodB
which calls a methodC
.现在,让我们说
methodA
调用methodB
它调用一个methodC
。
What I've done works well except that obviously I use references.我所做的一切都很好,只是显然我使用了引用。 So if
methodA
argument is modified in methodC
just before raising an exception, when I serialize the call stack, We'll see a call stack where methodA
has an argument with a wrong value (we'll see the value set in methodC
).所以,如果
methodA
参数在修改methodC
只是抛出一个异常之前,当我序列调用堆栈,我们将看到一个调用堆栈methodA
有一个错误值的参数(我们会看到在设定值methodC
)。
I want to fix this issue and I considered using:我想解决这个问题,我考虑使用:
deepcloning
deepcloning
可能会更快Is there any other way that would allow me to save the state of an object in time with greater performances than deepcloning
, or at least let me intercept all modifications brought to a specified object (like the VEH hooking or the LocationInterceptor Aspect)?有没有其他方法可以让我以比
deepcloning
更好的性能及时保存对象的状态,或者至少让我拦截对指定对象带来的所有修改(例如 VEH 挂钩或 LocationInterceptor Aspect)?
PS: I can't use IOnPropertyChange
or things like that because I can't touch any of the existing sourceCode except if I can implement them at runtime but I didn't see anywhere that it was possible. PS:我不能使用
IOnPropertyChange
或类似的东西,因为我不能接触任何现有的源代码,除非我可以在运行时实现它们,但我没有看到任何可能的地方。
Considering that I didn't want to implement any specific code in the object that needed to be monitered I had only 2 options :考虑到我不想在需要监控的对象中实现任何特定代码,我只有 2 个选项:
Serialization or any deepcloning method whenever a state need to be saved (I used JSON.NET for this solution)每当需要保存状态时的序列化或任何深度克隆方法(我在此解决方案中使用了 JSON.NET)
Logging all modification brought to the object in order to reverse them if necessary => to be notified when a modification will occur I explored all ths techniques:记录对对象进行的所有修改,以便在必要时将其反转 => 在发生修改时收到通知我探索了所有这些技术:
stfld
instruction. stfld
指令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.