Reflection method to Draw shape in C# winform -


i have blank method

void drawshape(graphics g, int x, int y){  } 

i want pass "method body" string on runtime, draw shape code, ex:

y =0; while(x<100){  x++;  g.drawrectangle(rec1, x,y); } 

and method execute like:

void drawshape(graphics g, int x, int y){     y =0;     while(x<100){      x++;      g.drawrectangle(rec1, x,y);     } } 

it's wonderful, don't know how that. appreciated

look @ dynamically compiling code, need pass entire class definition (including using statements).

codedomprovider cdp = codedomprovider.createprovider("c#"); compilerparameters cp = new compilerparameters(); cp.generateinmemory = true; cp.generateexecutable = false; cp.includedebuginformation = false; // add assemblies if required: // e.g. cp.referencedassemblies.add(...)  compilerresults cr = cdp.compileassemblyfromsource(cp, new string[] { sourcecode }); 

if there no errors (cr.errors) can access dynamically generated types:

    assembly = cr.compiledassembly;     type[] types = a.gettypes(); 

if code has 1 class definition, types have length 1. here, can invoke instance of type, , cast interface define.

e.g.

interface idrawxyz { void drawshape(graphics g, int x, int y); }  string srccode =@" using system; using system.drawing; public class myobject1 : idrawxyz {    //... }"; 

or can static methodinfo accepts graphics object , 2 int parameters.


Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -