When does the MethodHandle of an exception change?

I am trying to check if I can combine the exceptions together with some legacy code. I have a class Logger

that takes in an exception and logs it. Ideally, I want to get the unique ID (regardless of the text) where the exception was thrown.

I stumbled upon Exception.TargetSite.MethodHandle

. It seems to be an integer that I could use to do what I wanted. In debug, I threw an exception several times and noticed that the values ​​were static, big!

enter image description here

So I moved the exception to confirm that it will change, if the exception was different, the value changed and great!

I returned the exception to where it was originally and was checked again, waiting for the original integer value to return, but it didn't, it returned a completely different value. :(

It seems that the values ​​change every time the source is recompiled.

This brings me to my question. The documents MethodHandle

are not large :

Gets a handle to the method's internal metadata representation.

Thanks to Microsoft for very informative. I'm guessing it MethodHandle

is some kind of in-memory address for the method? I'm not sure though. Can anyone explain when and why this value changes? Is this a stamp in the assembly or is it a memory address on the stack or heap? Will it change when the process is recycled?

+3


source to share


1 answer


The value you see is a pointer to a structure MethodDesc

in the CLR that does not guarantee the same value. If you need to identify a method in a module, you can use it MetadataToken

, but it can change if you recompile the module.



+2


source







All Articles