4

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();
    }
}
Beetee
  • 475
  • 1
  • 7
  • 18
Ali Yousefi
  • 2,355
  • 2
  • 32
  • 47
  • How does your code look like? (which you want compile) – eMi Dec 24 '14 at 08:39
  • This is a pretty irregular requirement, you are usually able to work without such methods. Could you describe what you are trying achieve? Maybe there's an easier way for your case. – Moti Azu Dec 24 '14 at 08:44
  • thanks friend for replay. i want to check new version in my application from internet and download C# code from internet and show a message in my android application – Ali Yousefi Dec 24 '14 at 09:17
  • or run a new activity from my C# downloaded code – Ali Yousefi Dec 24 '14 at 09:23
  • Why do you need to compile c# in run time to check for new version of your app? Also running activity from dynamic c# code is pretty wild, could you explain further? – Moti Azu Dec 24 '14 at 09:43
  • i want to show user new activity (with style and back-color) and version features. i want to show this activity in a runtime code.or that i want to add a plugin from internet in my android application. – Ali Yousefi Dec 24 '14 at 10:12
  • That still doesn't explain WHY this needs to be done via dynamic code generation. – Moti Azu Dec 24 '14 at 14:20

1 Answers1

3

It's complicated 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. This means you might be able to work out certain code, but it won't work for any valid c# code.

Moti Azu
  • 5,392
  • 1
  • 23
  • 32