In my VB.NET application, the event AppDomain.CurrentDomain.AssemblyResolve
has a handler subscribed to it. The ResolveEventHandler
which is subscribed to this event was added upstream from my code (for all I know, System.AppDomain
has its own Private Method
subscribed to the Event)... is it possible to Remove all handlers from this event, so that I can add my own handler and ensure it's the only one?
Essentially I'm trying to do this:
RemoveHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf ClassX.MethodX
But I don't know what ClassX
or MethodX
are in this example because I haven't added a handler to this event yet, and this handler was added by upstream code. I'm using the method described here to check if any handler is subscribed event:
https://stackoverflow.com/a/2953318/734914
Edit: I was able to figure out which method is subscribed to the event, using the Immediate Window while debugging.
? DirectCast(gettype(System.AppDomain).GetField("AssemblyResolve", BindingFlags.Instance or BindingFlags.NonPublic).GetValue(AppDomain.CurrentDomain) , ResolveEventHandler)
{System.ResolveEventHandler}
_methodBase: Nothing
_methodPtr: 157334028
_methodPtrAux: 1827519884
_target: {System.ResolveEventHandler}
**Method: {System.Reflection.Assembly ResolveAssembly**(System.Object, System.ResolveEventArgs)}
Target: Nothing
Now I'm trying to Remove it, like this, because it's not a Public method:
RemoveHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf GetType(Reflection.Assembly).GetMethod("ResolveAssembly")
But that gives a compiler error saying: "AddressOf parameter must be the name of a method". So I'm not sure how to specify a non-Public method here