An object reference is required for the non-static field, method or property. While Compiling C# Code at Runtime -



An object reference is required for the non-static field, method or property. While Compiling C# Code at Runtime -

i sense bad asking when there many questions related not able understand...

i have public class "codecompiler" allows me compile , run c# code @ run-time. ide`s work.

when click "button1" creates code @ run-time, compiles , executes. main form1 contains textbox command called "textbox1". in order create changes on "textbox1" run-time made button1_click event. when click it, shows me run-time error...

compiler errors : line 14,34 : object reference required non-static field, method, or property 'compiling_csharp_code_at_runtime.form1.textbox1'

it showed me when editing text info on "textbox1". if seek create changes other property : "size", "location" imagine happen!

using system; using system.text; using microsoft.csharp; using system.codedom.compiler; using system.reflection; namespace compiling_csharp_code_at_runtime { public class codecompiler { public codecompiler() { } public object executecode(string code, string namespacename, string classname, string functionname, bool isstatic, string[] references1, params object[] args) { object returnval = null; compilerparameters compilerparams = new compilerparameters(); (int = 0; <= references1.getupperbound(0); i++) { compilerparams.referencedassemblies.add(references1[i]); } assembly asm = buildassembly(code, compilerparams); object instance = null; type type = null; if (isstatic) { type = asm.gettype(namespacename + "." + classname); } else { instance = asm.createinstance(namespacename + "." + classname); type = instance.gettype(); } methodinfo method = type.getmethod(functionname); returnval = method.invoke(instance, args); homecoming returnval; } private assembly buildassembly(string code, compilerparameters compilerparams) { microsoft.csharp.csharpcodeprovider provider = new csharpcodeprovider(); icodecompiler compiler = provider.createcompiler(); compilerparams.generateexecutable = false; compilerparams.generateinmemory = true; compilerresults results = compiler.compileassemblyfromsource(compilerparams, code); if (results.errors.haserrors) { stringbuilder errors = new stringbuilder("compiler errors :\r\n"); foreach (compilererror error in results.errors ) { errors.appendformat("line {0},{1}\t: {2}\n", error.line, error.column, error.errortext); } throw new exception(errors.tostring()); } else { homecoming results.compiledassembly; } } } public partial class form1 : form { public form1() { initializecomponent(); } private void button1_click(object sender, eventargs e) { codecompiler cc = new codecompiler(); string sourcecode1 = @" using compiling_csharp_code_at_runtime; using system; using system.componentmodel; using system.windows.forms; using system.drawing; namespace n1 { public class c1 { public static void f1(string st1, string st2) { compiling_csharp_code_at_runtime.form1.textbox1.text += ""this demo "" st1 + st2.toupper(); } } }"; string namespace1 = "n1", class1 = "c1", function1 = "f1"; bool isstatic = true; object o = cc.executecode(sourcecode1, namespace1, class1, function1, isstatic, new string[] { "compiling csharp code @ runtime.exe", "system.windows.forms.dll", "system.drawing.dll", "system.componentmodel.dll", "system.dll" }, "arg1", "arg2"); } } }

i found many problems related problem on site suggestion was...

"it looks calling non static property static method. should either create property static, or create instance of form1."

but creation instance of form1 hard in run-time!

the problem appears you're not passing reference form1 on want code executed codecompiler @ all. fact you're calling within form1 doesn't alter - objects don't automatically larn object using them. (if did, things lot more complicated.)

the way you're accessing form1 wrong - you're using typename (by way, fully-qualified path pointless, because while class c1 not in same namespace form1, include form1's namespace in compiled code via using) refer non-static fellow member of type. instance fellow member textbox1 of type form1 can accessed instance, there's no logical way access the form1 object. if you'd instantiated 10 of them? how decision made of 10 homecoming call?

what should do, if want go on downwards path (and i'm not sure why you're trying mimic eval() in c# - you're throwing away lot of safety makes c# nice , easy work with), pass reference form1 instance want altered either in constructor of codecompiler or in executecode method. think latter create more sense.

you should alter sourcecode1 looks (this fixes typo in original code, restoring missing + character):

string sourcecode1 = @" using compiling_csharp_code_at_runtime; using system; using system.componentmodel; using system.windows.forms; using system.drawing; namespace n1 { public class c1 { public static void f1(string st1, string st2, form1 formtoexecuteon) { formtoexecuteon.textbox1.text += ""this demo "" + st1 + st2.toupper(); } } }";

then phone call executecode() method this:

object o = cc.executecode(sourcecode1, namespace1, class1, function1, isstatic, new string[] { "compiling csharp code @ runtime.exe", "system.windows.forms.dll", "system.drawing.dll", "system.componentmodel.dll", "system.dll" }, "arg1", "arg2", this);

this compile code such form1 instance used can passed method when it's invoked. , reference form1 instance provided in argument list passed method invocation (i.e. lastly argument, this)..

granted, if works, it'll allow execute code on form1 objects. greater point of if you're going go downwards path, need passing reference want altered codecompiler somehow. i'm not sure whether object objecttochange work instead of formtochange, , how much info compiler going need object you're passing in order execute code on it.

and 1 time more, feeling: need very, unique utilize case create of remotely utilize of time or sanity. (you have pass 7 precisely-constructed objects, strings, executecode() every single time want run anything!)

c# reference runtime codedom non-static

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -