none
How can i show form Dynamically by passing string in roslyn project code

    Frage

  • 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);
            }
        }
    }
    
    
    Mittwoch, 7. Dezember 2011 11:05

Alle Antworten

  • 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);

    Mittwoch, 7. Dezember 2011 15:26