Override of Graphics.DrawLine

后端 未结 2 978
情歌与酒
情歌与酒 2021-01-28 13:16

I seek to @override the DrawLine method of the Graphics class, but I am unsure if this is possible?

I seek to put a validation in

相关标签:
2条回答
  • 2021-01-28 14:00

    It is not possible for normal code to replace method that is not designed to be replaced/overriden.

    You can

    • pre-process (i.e. ILDASM/modify/ILASM) all calling assemblies to use your new method
    • replace System.Drawing assembly with your own
    • use some existing framework that allows to override such methods like Moles or Fakes if you need it for unit testing.
    0 讨论(0)
  • 2021-01-28 14:16

    You just need a separate method. Put it wherever you want:

    void CheckedDrawLine(Graphics g, int x1, int y1, int x2, int y2, int stopY) {
        // Do the validation, draw the line
    }
    

    And you could make it into an extension method by sticking it in a static class:

    void CheckedDrawLine(this Graphics g, int x1, int y1, int x2, int y2, int stopY) {
        // Do the validation, draw the line
    }
    

    to do a g.CheckedDrawLine(...) instead of a CheckedDrawLine(g, ...).

    0 讨论(0)
提交回复
热议问题