Compiling C# at runtime

I’m not sure if this has been done before. If you have seen something like this before I would love it if you could send me a link. Otherwise…

A few months ago I was digging around to see if I could compile C# at run time because I had some need for it. Whether or not I actually needed it is a different debate, but I was surprised to find that I actually could (imagine me cackling with insane laughter, as I furiously copy-paste code examples and hack things around as they slowly start working. Yes! This is exactly what I wanted!).

One recurring problem I found in my research was that most people who have done this could only do it in the editor or if the Mono SDK was installed on the computer the game would later run on. This wouldn’t do; I very explicitly needed this to work at runtime and I didn’t want to bother anyone with an SDK installation every time I wanted to share a game I made with a friend through a dropbox link (Hey, can you check out my game? Can you download this monstrous SDK thing first? Oh, what’s an SDK? Don’t worry about it. Have you played it yet? What do you mean it’s still downloading? etc. Could you imagine?).

This is a problem I figured out how to overcome, as I describe here.

In case you are lazy and/or the site no longer exists because I’m a terrible person and I forgot to pay the yearly domain name fee, I’ve quoted it below:

This is really exciting and cool for me. However, there are a few problems with the method I found, and I would like to know if anyone knows anything about the following issues:

  • The mcs dll is very black box and the instructions are not easy to follow. I can’t know for sure if the dll is what it claims to be because it’s not from a reputable source and I can’t make one myself very easily because the steps require a lot of manual intervention. Is there a better way of doing this? Can anyone at least give a more detailed explanation of how to create the mcs dll?
  • From what I have read, the generated DLL is stored in memory… for forever. It can never be unloaded. This is terrible, especially if you plan on compiling stuff a lot. Further research lead me to Evan Todd’s blog where he mentions this issue, but what is an AppDomain?
  • I mentioned that the CLR behaves differently on different platforms, but I don’t know why. Why does “param.ReferencedAssemblies.Add(“System.dll”)” work on MacOSX, but not on my Windows machine? Is it a path thing?

There is another stray question I have: Aeroson mentions that Cities Skylines included the Mono SDK with their game in order to implement this sort of functionality. Is this true? And also, does Elite Dangerous do this too? I’ve noticed that Elite Dangerous requires me to install the Mono SDK in order to play their game (it’s part of the installation on MacOSX), but I don’t think Elite Dangerous has any mods… Is there a different reason for why Elite Dangerous needs the Mono SDK installed?

Anyway: Thoughts, questions, and comment are all appreciated.

1 Like

I am currently working on a project in which you code a cube’s AI.
I will let you know how to even start when I will finish it.

As i’ve been recently doing my research on this thread too, i will publish here some links that i found interesting:

I didn’t make any of them.
All of them rely on old Net 2.0