English 中文(简体)
简单即时窗口
原标题:Simple Immediate Window

I am building a little Immediate Window like the one in Visual Studio. I simply try to compile the Text in a Textedit. Now my Question: how do i get into the Scope of my running program? Is it possible to change Values or Lists of my programm? Thats my current compile code:

    private void SimpleButtonCompileClick(object sender, EventArgs e)
    {
        CompilerParameters compilerParameters = new CompilerParameters();
        compilerParameters.GenerateExecutable = true;
        compilerParameters.GenerateInMemory = true;
        compilerParameters.TreatWarningsAsErrors = true;
        compilerParameters.CompilerOptions = "/nowarn:1633";
        String sourceCode = "";
        string pattern =
                "\s*#pragma\s+reference\s+"(?<path>[^"]*)"\s*";
        System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(sourceCode, pattern);
        if (match.Success)
            compilerParameters.ReferencedAssemblies.Add(match.Groups["path"].Value);
        // Specify .NET version
        Dictionary<string, string> providerOptions =
                new Dictionary<string, string>();
        providerOptions.Add("CompilerVersion", "v3.5");
        CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);
        // Compile source           
        CompilerResults results = provider.CompileAssemblyFromSource(compilerParameters, new string[] {sourceCode});
        // Show errors
        if (results.Errors.HasErrors)
        {
            String errorString = "";
            foreach (var err in results.Errors)
                errorString += err + "
";
            XtraMessageBox.Show(errorString);
        }
        else
        {
            // Invoke compiled assembly s entry point
            results.CompiledAssembly.EntryPoint.Invoke
                    (null, new object[0] {});
        }
    }
问题回答

我理解你有一个方法( 按钮? 密钥组合? ), 可以生成一个表格, 输入您想要编译和执行的代码。 由于 Invoke 是在同一个过程和 AppDomain 中执行的, 只要您有一个访问点, 就可以访问运行中程序中的所有内容 。

例如,它可以是您班级中的一个中的公共静态变量。或者,要获得一个特定的表单,您可以使用应用程序。打开格式并通过窗体集合循环,寻找合适的类型。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签