No definition or extension method - ISupportInitialize
-
21-12-2019 - |
Question
I'm decompiling a dll into C#. One of the decompiler generated project contains 1000+ buggy lines which one like below:
this.pictureBox1.BeginInit();
Error 385 'System.Windows.Forms.PictureBox' does not contain a definition for 'BeginInit' and no extension method 'BeginInit' accepting a first argument of type 'System.Windows.Forms.PictureBox' could be found (are you missing a using directive or an assembly reference?)
There is no BeginInit() method for regular Picturebox. I simply commented out the lines:
//this.pictureBox1.BeginInit();
Then I've examined another decompiler's output, it looks like below and VS compiler hasn't previous error:
((ISupportInitialize)this.pictureBox1).BeginInit();
Because of a crash, I couldn't export all sources at once with the second decompiler. Now I'm not sure what I have to do. I couldn't run & debug the project because of other errors need to be cleared first. My options are:
- Just comment out the lines.
- Modify and insert (ISupportInitialize) tag.
- Create an extension method if possible.
Last option seems easiest to me. Can I create BeginInit() extension method, will it work without any problem? And how can I implement & use ISupportInitialize?
Solution
Try this:
public static class PictureBoxExtension {
public static void BeginInit(this PictureBox pb){
((ISupportInitialize)pb).BeginInit();
}
}
Or even better, you can try this for all controls supporting ISupportInitialize
. This is hidden from normal access because it's implemented explicitly, so we can use this extension to expose it (without casting):
public static class SupportInitExtension {
public static void BeginInit<T>(this T c) where T : ISupportInitialize {
c.BeginInit();
}
}