-1

I am working with specific program. It has "main window" (a.k.a. "form") where another extensions(protected dll, without access to their source code) can add some objects (texts, labels and etc..) in that window.

How can I access all the objects which is added on that "form" ?

here i example of typical extension:

....
namespace ProgramName.Extensions {

    public class MyExtensionName : ProgramName.ExtensionBase{

         //here goes typical codes to add things to "form" window,like:
         Draw.Arrow(MainFormObject, ArrowColor, ArrowWidth)

    }

}

what code I can use to access all "add"-ed elements , from other, anonymous/inaccessible classed? Is there any "reflection" or any solution?

T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • Depends on what that "form" is. If that's WPF window or WinForms form - you can just ask it about its children. – Evk Oct 31 '17 at 09:12
  • Depends; this really cannot be answered without knowledge of the 3pty library. Are they just drawing the objects to canavas - then you're out of luck ? Or are they holding reference somewhere ? You can take a tool like IlSpy and take a look at the library - figure out yourself. – Ondrej Svejdar Nov 01 '17 at 09:54
  • More information about involved classes is needed. For example, what is that Form class? Can you just derive from it and override the AddXXX Method(s)? – Ola Berntsson Nov 01 '17 at 12:20

3 Answers3

2

Adding some objects on a form using a dll requires that the form itself be referenced as an accessible member using something like:

 Assembly assembly = Assembly.LoadFile("C:\\test.dll");
Type type = assembly.GetType("test.dllTest");
Form form = (Form)Activator.CreateInstance(type);

Then add:

form.Controls.Find();

If it is the current form, then on Load method use:

 private void Form_Load(object sender, EventArgs e)
    {
      this.Controls.Find("name",true)
      // OR
      foreach(Control c in this.Controls)
        if(c.Name == Name)
            //Do something...
    }
TiyebM
  • 2,684
  • 3
  • 40
  • 66
1

You can try this

foreach (Control ctrl in this.Controls)
{
    if (ctrl .GetType().GetProperty("Text") != null)
    {
        // code here            
    }
}
thehennyy
  • 4,020
  • 1
  • 22
  • 31
1

protected dll, without access to their source code

  • These controls sound like COM/OLE/ActiveX:
  • You can access them, as long as you find the code wherein the Main form(The host program) they are maintained.
  • You can try search those controls' keywords in your project code to locate the relevant codes.
  • Including those DLLs into your project reference would provide you the chance to inspect keywords from DLLs.
  • Search the usage of these DLLs and find the code place you want to work with and do your things there.

Example how to get keywords out of DLLs:

enter image description here

Richard
  • 96
  • 10