Problem with System.CodeDom in C#

Hey,

I'm currently playing around with dynamic c# code creation, which I've used successfully in non-Unity programming. However, when I try to use any classes from the System.CodeDom namespace Unity doesn't seem to be able to find it. For example, the following line of code:

System.CodeDom.Compiler.CompilerParameters compilerParams;

Gives the following error:

Assets/ScriptLoader.cs(8,33): error CS0234: The type or namespace name `CompilerParameters' does not exist in the namespace`System.CodeDom.Compiler'. Are you missing an assembly reference?

I've checked which assembly System.CodeDom is located in and it seems it should be System.dll. The Unity Script Reference for Mono Compatibility also seems to indicate that System.CodeDom should be supported.

All in all, I have no idea what, if anything, I am doing wrong, and any help would be greatly appreciated.

Thanks,

Wibbs

2 Answers

2

For anyone wondering, the fix is to explicitly set the api compatibility level to .NET 4.x under Edit → Project Settings → Player

Thank you so much.

Hmm, just tried it and it seems that it works in an editor script but not in a runtime script. I haven't used System.CodeDom yet so i don't know anything about it, but i guess it's not supported by the Unity-Engine.


edit

Just figured it out :wink:

My test project works still with the “.NET subset” option. Just go to EditProject SettingsPlayer and select the .NET option under Optimization.

After recompiling the error was gone.

@JK_Aahz: Uhm, this question was explicitly about using System.CodeDom at runtime. I said in my answer that it works in the editor but not at runtime without changing the .NET level. Editor script have to be inside an editor folder, otherwise they aren't editor scripts but runtime script and will be included in a build.

Oh, I realize that, it's just that there are only a handful of Unity + CodeDom references floating around when you're searching. This is one of the top links with a relatively high view/comment ratio, so figured it couldn't hurt to add additional info (which is why I said 'side note'). This question/answer helped me figure out my problem even though I didn't need runtime usage of CodeDom, so might be others out there with a similar problem needing further hints and explanation.