Can be used monodroid for compiling C# code on Android device? I would like run C# code on the fly on my Android tablet, the monodroid looks like possible solution, but I didn't found any notice if the monodroid compiler can run only on windows/mac or on the Android too.
2 Answers
I was originally going to post this as a comment on Chris Sinclair's answer, but figured it was going to end up long enough that it may as well be a separate answer.
I wrote C# to Go, so I can provide some commentary there. It is in fact written using Mono for Android. However, it is not correct to say that Mono for Android does a conversion or compilation from C# into Java. C# code within an application runs on top of the Mono runtime, and callable interfaces are used to talk back and forth between runtimes (see this answer for more details on that).
Back when I first released C# to Go last year I wrote up a blog post describing how I put it together. Basically, I am using a version of Mono's C# compiler service that I compiled against the Mono for Android profile. All the source for the app is available on GitHub as well.
It's not a perfect solution as there are some problems with the current implementation (some of which are detailed in the blog post). Currently I'm unable to support creating/compiling classes in the app due to a problem in the Mono runtime. This is a documented problem that has been fixed in later versions of Mono, but since Mono for Android is still based off Mono 2.10 it has not gotten those fixes. My understanding is that later this year Xamarin will begin working on bringing Mono for Android (and also MonoTouch) up to Mono 2.12 which should also bring with it these fixes, plus the C# compiler service right in the box, avoiding the need to bring it in as a separate library as I did.

- 1
- 1

- 10,009
- 2
- 29
- 35
EDIT: See comments by Greg Shackles as he goes further into the process and corrects my misunderstandings.
There's an Android app C# To Go which does exactly that and I've used it before. I can't say for certain if the author used Mono for Android or not (wouldn't be surprised). Perhaps you can contact the author of that app. However, as I understand it, Mono for Android pre-compiles the DLLs and converts them into Java or somehow wires them into the Java interface before packaging/deploying. I don't know if generating an assembly via a compiler at runtime on the platform would yield a useable assembly out-of-the-box that you could load.
Beyond that, I know that pseudo-code generators that emit IL at runtime (rather than compiling DLLs) work as I'm using them. In particular, I'm using a C# port of FLEE (Fast Lightweight Expression Evaluator) on Mono for Android. Perhaps something like that can be sufficient for your needs.
Finally, investigate Mono's own compiling CodeDOM features. From what I understand, it has a better code compiler service for runtime (similar to what C# 5 will be bringing us); perhaps it's available for Mono for Android as well.

- 22,858
- 3
- 52
- 93
-
1Mono for Android does not pre-compile C# into Java, so that part is incorrect. I'm the author of C# to Go, so I just posted an answer that gives some insight into the app as well. – Greg Shackles Sep 17 '12 at 13:07
-
Thanks for the insight @GregShackles, and great job on C# To Go! – Chris Sinclair Sep 17 '12 at 16:25