首页 > 解决方案 > Xamarin.Forms 中的动态 c# 编译器找不到类型或命名空间

问题描述

我有一个 Xamarin.Forms 应用程序可以在按下按钮时动态编译 c# 代码,但我在代码中遇到错误。

这是按下按钮的代码:

        {
            string text = ((Editor)sender).Text;
            var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
            var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "foo.exe", true);
            parameters.GenerateExecutable = true;
            CompilerResults results = csc.CompileAssemblyFromSource(parameters, text);
            results.Errors.Cast<CompilerError>().ToList().ForEach(error => string errors = error.ErrorText;
        }

我正在使用:

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Microsoft.CSharp;
using System.CodeDom.Compiler;

XAML 是这样的: <Button x:Name="compilebutton" Text="Compile" Clicked="oncompile" />


The type or namespace name 'CompilerParameters' could not be found (are you missing a using directive or an assembly reference?)    

The type or namespace name 'CompilerResults' could not be found (are you missing a using directive or an assembly reference?)   

The type or namespace name 'CompilerError' could not be found (are you missing a using directive or an assembly reference?) 

我已经在没有 Xamarin.Forms 的控制台应用程序中对此进行了测试,它运行良好,我不确定问题是什么,任何帮助将不胜感激,谢谢!

标签: c#xamarinxamarin.forms

解决方案


推荐阅读