Is it possible to intercept a static method on an object that you do not own and did not create?

Referring to my possible answer to this question: How would you check the ASP.NET membership tables when recording that the user made changes?

Is it possible to intercept a call coming from code that you are not in ctor on a closed inner class that you do not have, in order to manipulate the object before returning?

Specific example:

SqlMembershipProviderFor all data access, it creates an instance of the auxiliary connections System.Web.DataAccess.SqlConnectionHolder.

The desired result is to intercept this instance and execute the operation on a public connection that opens in ctor System.Web.DataAccess.SqlConnectionHolderbefore continuing with the execution.

UPDATE: So, as leppie remarked on my example, what I say, I want, is not at all what I want.

Goal now System.Web.DataAccess.SqlConnectionHelper.GetConnection()

So, is it possible to intercept the call to this method?

internal static SqlConnectionHolder GetConnection(string connectionString, bool revertImpersonation)

It is possible. If so, a brief example will be given.

+3
source share
1 answer

Technically, this is possible using IL rewriting, similar to what Postharp does. However, given that this is a .NET assembly, you are saying that it has a strong name, is stored in the GAC and ngen-ed, which is unlikely to ever overcome the “technically possible” stage.

+2
source

Source: https://habr.com/ru/post/1747174/


All Articles