Change the internal implementation of a .NET class method

I would like to change the way my C # /. NET application works. I dug up the .NET platform with Reflector and found a pretty good place where I could use a different implementation of the method. This is the inner class in the System.Windows.Forms namespace. Obviously, you cannot change the code of this class by conventional means, so I thought it would be possible to replace the method there through reflection at run time. The method that I would like to completely replace for my application is this:

public static WindowsFontQuality WindowsFontQualityFromTextRenderingHint(Graphics g)

in the class:

internal sealed class System.Windows.Forms.Internal.WindowsFont

Is there a way to load this type and replace the method at runtime without affecting other applications that are currently running or running? I tried loading the type Type.GetType()and similar things, but still failed.

+3
source share
5 answers

You may be able to do this using the debugger API, but this is a really really bad idea.

On the one hand, working with installed debugger hooks can be slower - but more importantly, interfering with the framework code can easily lead to unexpected behavior. Do you know exactly how this method is used in all possible scenarios even in your own application?

It could also have undesirable legal consequences, although you should consult with a lawyer about this.

I personally would give up this line of thinking and try to devise another way to accomplish what you are trying to do.

+8
source
  • , , , , , .NET Framework.
  • , , ( , , ).

: , Core Framework , Mono, , .:)

+3

, . , Mono Cecil . .

, : .

+1

Although I can not comment on Myra because of the reputation. Using extension methods is wrong here, as TS is trying to replace the whole .Net function, which is really a very bad idea.

Try to inherit the class you want to change and override the corresponding properties.

0
source

Make a Microsoft feature request if your change is of common interest.

0
source

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


All Articles