How can i show form Dynamically by passing string in roslyn project code
-
7 ธันวาคม 2554 11:05
hi,
i want to show form by giving form name as string in roslyn code but i am getting some error on below code.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Roslyn.Compilers; using Roslyn.Compilers.CSharp; using Roslyn.Scripting; using Roslyn.Scripting.CSharp; namespace WindowsFormsApplication7 { public partial class Form1 : Form { public Form1() { InitializeComponent(); ScriptEngine scriptEngine = new ScriptEngine(); Session mySession = Session.Create(); string f =""; f = @" Form2 frm = new Form2(); frm.Show();"; scriptEngine.Execute(f); } } }
ตอบทั้งหมด
-
7 ธันวาคม 2554 15:26
The ScriptEngine has its own set of assembly references and usings. So, if you want to instantiate the form, you can do something like this:
ScriptEngine scriptEngine =
new ScriptEngine(
new[]
{
Assembly.GetExecutingAssembly(),
typeof(Form).Assembly, // System.Windows.Forms
typeof(Uri).Assembly // System
});
string f = @"
using WindowsFormsApplication7;
Form2 frm = new Form2();
frm.Show();";
scriptEngine.Execute(f);- แก้ไขโดย svick 7 ธันวาคม 2554 15:27 fixed namespace
- เสนอเป็นคำตอบโดย Shyam NamboodiripadMicrosoft, Owner 7 ธันวาคม 2554 20:16