winforms - Best practice to write highly user extendable C# application -


i @ research institute micromaching/fine mechanics , asked develop controller software current setup use in 1 of our labs.

we have nano stage , other equipment shutters , filter wheels should controlled central application. software provides pre-configured jobs execution, algorithms generate commands stage , used write different patterns laser samples (for example rectangles, cylinders, etc.)

now great provide kind of possibility extend list of predefined jobs on runtime, means algorithms ones provided can added user.

i'm c# newbie (and desktop application newbie in general) extremely thankful if can give me hints on how done or should start looking.

i did 'script' thing using .net integrated c# compiler.
work looks this:

    public assembly compile(string[] source, string[] references) {         codedomprovider provider = new csharpcodeprovider();         compilerparameters cp = new compilerparameters(references);         cp.generateexecutable = false;         cp.generateinmemory = true;         cp.treatwarningsaserrors = false;          try {             compilerresults res = provider.compileassemblyfromsource(cp, source);             // ...             return res.errors.count == 0 ? res.compiledassembly : null;         }         catch (exception ex) {             // ...             return null;         }     }      public object execute(assembly a, string classname, string methodname) {         type t = a.gettype(classname);         if (t == null) throw new exception("type not found!");         methodinfo method = t.getmethod(methodname, bindingflags.public | bindingflags.nonpublic | bindingflags.instance);          // method         if (method == null) throw new exception("method '" + methodname + "' not found!");                                  // method not found          object instance =  activator.createinstance(t, this);         object ret = method.invoke(instance, null);          return ret;     } 

the real code lot more things, including code editor.
works years in our factory.

this way users use c# scripts , can therefore use same api do.

you can use code template looks plain .cs file. build @ runtime , provided source parameter of compile.

using system; using system.io; using ...  namespace mycompany.stuff {     public class scriptclass {         public object main() {              // copy user code here             // call own methods here          }          // or copy user code here          private int test(int x) { /* ... */ }     } } 

example:

string[] source = ??? // code textboxes, files or whatever, build template file... string[] references = new string[] { "a.dll", "b.dll" };  assembly = compile(source, references); object result = execute(a, "mycompany.stuff.scriptclass", "main"); 

Comments

Popular posts from this blog

javascript - JS causing window size to be bigger than necessary - Dropdown bug -

php - Calling a template part from a post -

How to mention the localhost in android -