Use methods of a form from runtime-compiled code

Viewed 37

I have a program that have a log window (named FormLog), where I can easily write logs from other windows, using following syntax:

(Application.OpenForms["FormLog"] as FormLog).AddText(text);

My program also have ability to use user generated C# code, that will be compiled runtime, and it uses CSharpCodeProvider.CompileAssemblyFromFile method. So basically user can change part of my code for special purposes (like fixing website crawling things). All this is already working fine.

The problem is, how can I access FormLog.AddText method from user generated code? I can find my FormLog from user generated code when I iterate through all open forms:

foreach (Form item in Application.OpenForms)

So Application.OpenForms method finds my FormLog, but I can't cast it as FormLog because for obvious reasons name FormLog is unknown form in user generated code (I get error The type or namespace name 'FormLog' could not be found). And if I don't cast it, then the method WriteText could not be found ('System.Windows.Forms.Form' does not contain a definition for 'WriteText').

So how can I access FormLog.WriteText method from code that is compiled at runtime?

I know this Application.OpenForm is not best approach to access other forms in general, but that's how it's already build, and I just try to get it working from user generated code.

0 Answers
Related