نوشته شده توسط
navidkhalilian
سلام.من به جای تابع addbutton این تابع رو اضافه کردم.اما نمیدونم چطوری باید خروجی تابع رو برگردونم.میشه راهنمایی کنید؟
سلام.
بدین شکل عمل کنید:
CSharpCodeProvider compiler = new CSharpCodeProvider();
CompilerParameters references = new CompilerParameters();
references.ReferencedAssemblies.Add("System.dll");
references.ReferencedAssemblies.Add("System.Window s.Forms.dll");
references.ReferencedAssemblies.Add("System.Drawin g.dll");
references.GenerateInMemory = false;
string lcCode = @"using System;
using System.Windows.Forms;
namespace MyNamespace {
public class MyClass {
public int Sum(Form parent, int a, int b) {
return a + b;
}
}
}";
CompilerResults compilerResults = compiler.CompileAssemblyFromSource(references, lcCode);
if (compilerResults.Errors.HasErrors)
return;
Assembly assembly = compilerResults.CompiledAssembly;
object obj = assembly.CreateInstance("MyNamespace.MyClass");
if (obj == null)
return;
object returnValue = obj.GetType().InvokeMember("Sum", BindingFlags.InvokeMethod, null, obj, new object[] { this, 10, 20 });
if (returnValue != null)
{
int sum = Convert.ToInt32(returnValue);
}
موفق باشید.