简体   繁体   English

如何在Xamarin Android中运行时执行C#代码?

[英]How to execute C# code at runtime in Xamarin Android?

I have a android application in Xamarin Studio. 我在Xamarin Studio中有一个Android应用程序。 I want to execute a code placed in the text (string). 我想执行文本中的代码(字符串)。 For example this question help me in Visual Studio Windows application. 例如, 这个问题在Visual Studio Windows应用程序中帮助我。 But I cannot use this answer in Xamarin Android. 但是我不能在Xamarin Android中使用这个答案。 This is my example in C# Windows application: 这是我在C#Windows应用程序中的示例:

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();
    }
}

It's complicated http://developer.xamarin.com/guides/android/advanced_topics/limitations/ 这很复杂http://developer.xamarin.com/guides/android/advanced_topics/limitations/

Since applications on Android require generating Java proxy types during the build process, it is not possible to generate all code at runtime.

From the Limited Dynamic Language Support and Limited Java Generation Support you can learn more about what specifically isn't supported. Limited Dynamic Language SupportLimited Java Generation Support您可以了解更多有关不支持的内容。 This means you might be able to work out certain code, but it won't work for any valid c# code. 这意味着您可能能够计算出某些代码,但它不适用于任何有效的c#代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM