Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify an internal .NET class' method implementation

I would like to modify the way my C#/.NET application works internally. I have dug into the .NET framework with Reflector and found a pretty good place where I could use a different implementation of a method. This is an internal class in the System.Windows.Forms namespace. You obviously cannot alter the code of this class with the usual means so I thought it would be possible to replace a method in there through reflection at runtime. The method I would like to entirely 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 any way to load that type and replace the method at runtime, not affecting any other applications that are currently running or started afterwards? I have tried to load the type with Type.GetType() and similar things but failed so far.

like image 958
ygoe Avatar asked May 23 '26 05:05

ygoe


2 Answers

You may be able to do this with the debugger API - but it's a really, really bad idea.

For one thing, running with the debugger hooks installed may well be slower - but more importantly, tampering with the framework code could easily lead to unexpected behaviour. Do you know exactly how this method is used, in all possible scenarios, even within your own app?

It would also quite possibly have undesirable legal consequences, although you should consult a lawyer about that.

I would personally abandon this line of thinking and try to work out a different way to accomplish whatever it is you're trying to do.

like image 52
Jon Skeet Avatar answered May 24 '26 18:05

Jon Skeet


  1. Anything you do to make this happen would be an unsupported, unreliable hack that could break with any .NET Framework update
  2. There's another, more correct, way to do what you are trying to accomplish (and I don't need to know what you're trying to do to know this for certain).

Edit: If editing core Framework code is your interest, feel free to experiment with Mono, but don't expect to redistribute your modifications if they are application-specific. :)

like image 30
Sam Harwell Avatar answered May 24 '26 19:05

Sam Harwell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!