如何在运行时在 Xamarin Android 中执行 C# 代码? [英] How to execute C# code at runtime in Xamarin Android?

查看:32
本文介绍了如何在运行时在 Xamarin Android 中执行 C# 代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Xamarin Studio 中有一个 android 应用程序.我想执行放置在文本(字符串)中的代码.例如 this 问题在 Visual Studio Windows 中帮助我应用.但是我不能在 Xamarin Android 中使用这个答案.这是我在 C# Windows 应用程序中的示例:

I have a android application in Xamarin Studio. I want to execute a code placed in the text (string). For example this question help me in Visual Studio Windows application. But I cannot use this answer in Xamarin Android. This is my example in C# Windows application:

public class CodeLuncher
{
    public static void LunchCSCode(string site, string typeName, string methosName)
    {
        try
        {
            var provider = CSharpCodeProvider.CreateProvider("c#");
            var options = new CompilerParameters();
            string text = new System.Net.WebClient().DownloadString(site);

            foreach (var item in GetRefrences(text))
            {
                options.ReferencedAssemblies.Add(item);
            }
            string code = GetCode(text);
            var results = provider.CompileAssemblyFromSource(options, new[] { code });
            if (results.Errors.Count > 0)
            {
                foreach (var error in results.Errors)
                {
                    Console.WriteLine(error);
                }
            }
            else
            {
                var t = results.CompiledAssembly.GetType(typeName);
                t.GetMethod(methosName).Invoke(null, null);
            }
        }
        catch
        {

        }
    }


    static string[] GetRefrences(string text)
    {
        Regex regExp = new Regex("<Refrences>(.*?)</Refrences>", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);
        string str = regExp.Match(text).Groups[1].Value;
        List<string> retText = new List<string>();
        foreach (var item in str.Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
        {
            retText.Add(item);
        }
        return retText.ToArray();
    }

    static string GetCode(string text)
    {
        Regex regExp = new Regex("<CSharpCode>(.*?)</CSharpCode>", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);
        string str = regExp.Match(text).Groups[1].Value;
        return str.Trim();
    }
}

推荐答案

很复杂 http://developer.xamarin.com/guides/android/advanced_topics/limitations/

由于 Android 上的应用程序需要在构建过程中生成 Java 代理类型,因此不可能在运行时生成所有代码.

Limited Dynamic Language SupportLimited Java Generation Support 中,您可以了解更多关于不支持的内容.这意味着您可能能够计算出某些代码,但它不适用于任何有效的 c# 代码.

From the Limited Dynamic Language Support and Limited Java Generation Support you can learn more about what specifically isn't supported. This means you might be able to work out certain code, but it won't work for any valid c# code.

这篇关于如何在运行时在 Xamarin Android 中执行 C# 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆