2

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:

  1. Just comment out the lines.
  2. Modify and insert (ISupportInitialize) tag.
  3. 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?

Nime Cloud
  • 6,162
  • 14
  • 43
  • 75
  • I think solution 2 is the "right" one. At least that's what the designer does. Create a form with some controls and you'll see a lot of lines like `((ISupportInitialize)this.pictureBox1).BeginInit();` in the `InitializeComponent()` method. – PMF Nov 17 '13 at 20:39

1 Answers1

4

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();
   }
}
King King
  • 61,710
  • 16
  • 105
  • 130
  • Error: Extension method must be defined in a top level static class; PictureBoxExtension is a nested class. – Nime Cloud Nov 17 '13 at 20:49
  • @NimeCloud yes, you should try searching on `extension method`. That's because you don't know how to use it. It should be placed in the outermost namespace. – King King Nov 17 '13 at 20:51
  • Ok got, it. It was the second level static class, now it's ok. When I confirm, I'll mark as answer. – Nime Cloud Nov 17 '13 at 20:52
  • SupportInitExtension would be better bec. of there are many different controls use BeginInit & EndInit. – Nime Cloud Nov 17 '13 at 20:55
  • 1
    @NimeCloud I also updated it with the better code, you can call the `BeginInit` directly on any control implementing `ISupportInitialize`. You can also do the same for `EndInit`. – King King Nov 17 '13 at 20:56
  • @NimeCloud consider accepting the answer, it would be a good encouragement for any answerer :| – King King Nov 17 '13 at 20:59
  • 1
    For .NET 2.0 extension methods: http://stackoverflow.com/questions/1522605/using-extension-methods-in-net-2-0 – Nime Cloud Nov 20 '13 at 12:03