如何从Roslyn Runtime类访问Form的控件?

时间:2016-10-17 11:23:26

标签: c# dynamic runtime roslyn

我制作了一个Roslyn脚本,以便在运行时执行它 我的意思是在Form1工作过程中点击按钮1时 在此事件之后我们需要弄清楚如何在Roslyn运行时获取textBox1.Text属性并将其用于我们的目的。

反正.....
首先,我们需要为我们的项目“DemoFormControlAccesInRoslyn”添加3个主要参考文献 这些参考文献是:

Microsoft.CodeAnalysis.CSharp     
Microsoft.CodeAnalysis    
System.Collections.Immutable.dll      
private void button1_Click(object sender, EventArgs e)
{
    string expresion1 = roslynExpresion;  //  it is down below

    var tree = SyntaxFactory.ParseSyntaxTree(expresion1);
    var comp1 = CSharpCompilation1(tree);
    Diagnostics1(comp1);

    ExecuteCommand(comp1, "Method1", this);
}
private static object ExecuteCommand(CSharpCompilation comp1, string MethodName1, params object[] P)
{
    object result1 = null;
    using (var stream = new MemoryStream())
    {
        comp1.Emit(stream);
        var assembly1 = Assembly.Load(stream.GetBuffer());
        var type1 = assembly1.GetType("RuntimeClass1");
        var greeter1 = Activator.CreateInstance(type1);
        var method1 = type1.GetMethod(MethodName1);
        try
        {
            method1.Invoke(greeter1, P);
        }
        catch (Exception ex1)
        {
            MessageBox.Show(ex1.ToString());
        }
    }
    return result1;
}
private static CSharpCompilation CSharpCompilation1(SyntaxTree tree)
{
    var option1 = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
    var ref1 = new[]
    {
        MetadataReference.CreateFromFile(typeof (System.ComponentModel.Component).Assembly.Location),
        MetadataReference.CreateFromFile(typeof (object).Assembly.Location),
        MetadataReference.CreateFromFile(typeof (FileInfo).Assembly.Location),
        MetadataReference.CreateFromFile(typeof (System.Linq.Enumerable).Assembly.Location),
        MetadataReference.CreateFromFile(typeof (System.Windows.Forms.Form).Assembly.Location),


        MetadataReference.CreateFromFile(typeof (Form1).Assembly.Location)
    };
    var comp1 = CSharpCompilation.Create("DemoRoslynRuntime").WithOptions(option1).AddReferences(ref1).AddSyntaxTrees(tree);
    return comp1;
}
private static void Diagnostics1(CSharpCompilation comp1)
{
    var diag1 = comp1.GetDiagnostics();
    foreach (var d in diag1)
    {
        if (d.WarningLevel < 3)
        {
            MessageBox.Show(d.ToString());
        }
    }
}

public string roslynExpresion =
@"
  using System;
  using System.Linq;
  using System.IO;
  using System.Collections;
  using System.Collections.Generic;
  using System.Windows.Forms;
  using DemoFormControlAccesInRoslyn;

  public class RuntimeClass1 : DemoFormControlAccesInRoslyn.Form1
  {
        public void Method1(Form1 f1)
        {
             MessageBox.Show(textBox1.Text);   //This will work if textBox1 has public modifier. But this will not work for dynamic!
             MessageBox.Show(f1.textBox1.Text);  //we can use this if textBox1 has public modifier.
        }
  }
"

总之...
我的目标是如何在我的Roslyn脚本表达式中解决这个问题? 当Form1运行时,我将textBox1.Text从“oldText”更改为“newText”,之后我将单击button1 Roslyn脚本将执行,但它将显示MessageBox仍具有“oldText”字符串值!
为什么呢?

另外为了做到这一点,是否需要从DemoFormControlAccesInRoslyn.Form1继承RuntimeClass1?
或者我们可以简单地从Form类继承它?

如果代替(f1.textBox1.Text)我们可以在Method1中编写(textBox1.Text),这可能会很好。
请记住这是我们的首要任务。

public void Method1(Form1 f1)
{
    textBox1 = f1.textBox1;  
    MessageBox.Show(textBox1.Text);  //this will work for dynamic if textBox1 has public modifier and we will see the message with current textBox1.Text value I mean "newText" string.
}

0 个答案:

没有答案