I thought I would offer what can about the topic of compiling C# in Unity. Just for full disclosure, I am the publisher of Roslyn C# - Runtime Compiler asset mentioned previously in the thread.
First thing I will say is that you can certainly compile C# in Unity without a paid asset, but it will take some research and learning on the subject as it is by no means trivial.
You mention you are trying to use the Code Dom in your initial post. From experience there are a few reasons why I would not use it, and unless things have changed in .Net Standard 2.1/Unity 6, then the Code Dom is using the Mono MCS compiler under the hood. The problems with that are:
- The MCS compiler is not available as part of the runtime on most platforms. It means you will need to ship the compiler packaged with your game in order for things to work, but even then due to the way it is implemented, the Code Dom implementation provided by Mono is using a fixed path where it is trying to locate the compiler binaries. For that reason it wont work as a standalone on most platforms because most users will not have the Mono compiler installed. It can be solved in other ways but it is a lot of pain and testing for not much gain.
- Another reason is that the MCS compiler does not support the latest C# language features. It may have changed a bit by now, but last I checked it was essentially the same as .Net 3.5 compatibility level from Unity 5 days or similar (Even simple things like the
in keyword were not yet supported just as an example). May have been updated since, but it will always lag behind the official C# standard by some margin because Unity cannot keep the runtime updated at a reasonable pace since the C# language is evolving so fast.
- I believe CodeDom (Or at least the compiler aspect) is depreciated in newer .net.
This is coming from previous experience because many years ago I created another paid asset named Dynamic C# which used MCS compiler internally. I found out all of these issues along the way (I originally started with the CodeDom just like you), and in the end I had to find a way to ship the MCS compiler as part of the game which required extensive modification.
With that said, the modern and much more portable option is to use Microsoft.CodeAnalysis known as Roslyn. It provides a C# API for analyzing, generating and compiling C# code, and since it is consumed as a library, then the dependencies get packaged automatically as part of your game so it can run on all platforms without extra installation steps. It is the official compiler for the C# language, so of course it supports all the latest language features, and internally this is what the paid asset is built on top of, but the paid asset just aims to make things as easy as possible while providing many other features like code and execution security, limited IL2CPP support via extension and more.
You will find that CodeAnalysis is free and open source under the MIT license, so can be used without restrictions. It will just need to be installed manually along with dependencies, which essentially means dropping the .Net Standard 2.1 version of the binaries into your project. Since nuget packages are essentially zip files, you can just download the latest version and all dependencies from the above link and extract to access those binaries (Make sure you include all dependencies recursively or Unity will complain about missing assemblies).
Once you have it installed in your Unity project, you will then need to read up on CodeAnalysis in relation to compilation. Unfortunately it is quite a bit more involved than the Code Dom to get things working since CodeAnalysis is quite a verbose/low-ish level API to work with. The documentation will be helpful there, and also it ChatGPT or similar can give good code examples to get you started if you ask how to compile C# code using Roslyn for example. Essentially there are 2 main steps to get this working which are parsing (Load a C# source file or string and convert it to a syntax tree) and emitting (provide a syntax tree, reference assemblies and further metadata so that the compiler can produce the output assembly or executable), so will be worth searching around with those keywords to get more info.
Hopefully that will get you started on a better path than MCS, which will no doubt cause you more issues down the line when you come to realize that it does not work at runtime for the most part without extensive modification.
For now the only limitation with Microsoft.CodeAnalysis(Or more a limitation of IL2CPP) is that it it will not be possible to execute the compiled code on any AOT platforms. Essentially it means you are limited to using the Mono backend, which equates to Windows, Linux and Mac. The paid asset can support IL2CPP with limitations using a free MIT add-on, but I will assume using Mono backend will be fine for your needs up to now.
I hope that can provide some help to you. As other users have mentioned, it is an advanced topic and will require research and leg work to get things setup and working, but no doubt it is possible for anyone paid or free thanks to Roslyn compiler platform. I have not covered the security aspect since you mention you do not care about it, but just be aware that executing external code on the user device particularly from an unknown/third party source (Modding or similar) can be dangerous and place the user device at risk. If you know the origin of the source code 100% (Content delivery or similar) then it is not so much of an issue but should always be a consideration.