- Home /
Question by
junsungkim04 · Jun 05 at 09:58 AM ·
c#errorcompile
NotSupportedException: Microsoft.CSharp.CSharpCodeProvider::.ctor error
It works without errors in the editor.
But in post-build debugging I get that error.
I don't know how to fix it.
Any help would be appreciated
An error occurred in that code.
using UnityEngine;
using System;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Text;
public class test : MonoBehaviour
{
private void Start()
{
var assembly = Compile(@"
using UnityEngine;
using System.Collections;
public class RuntimeCompiled : MonoBehaviour
{
public static RuntimeCompiled AddYourselfTo(GameObject host)
{
return host.AddComponent<RuntimeCompiled>();
}
void Start()
{
Debug.Log(""TEST"");
}
}");
var runtimeType = assembly.GetType("RuntimeCompiled");
var method = runtimeType.GetMethod("AddYourselfTo");
var del = (Func<GameObject, MonoBehaviour>)
Delegate.CreateDelegate(
typeof(Func<GameObject, MonoBehaviour>),
method
);
// We ask the compiled method to add its component to this.gameObject
var addedComponent = del.Invoke(gameObject);
// The delegate pre-bakes the reflection, so repeated calls don't
// cost us every time, as long as we keep re-using the delegate.
}
public static Assembly Compile(string source)
{
// Replace this Compiler.CSharpCodeProvider wth aeroson's version
// if you're targeting non-Windows platforms:
var provider = new CSharpCodeProvider();
var param = new CompilerParameters();
// Add ALL of the assembly references
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
param.ReferencedAssemblies.Add(assembly.Location);
}
// Or, uncomment just the assemblies you need...
// System namespace for common types like collections.
//param.ReferencedAssemblies.Add("System.dll");
// This contains methods from the Unity namespaces:
//param.ReferencedAssemblies.Add("UnityEngines.dll");
// This assembly contains runtime C# code from your Assets folders:
// (If you're using editor scripts, they may be in another assembly)
//param.ReferencedAssemblies.Add("CSharp.dll");
// Generate a dll in memory
param.GenerateExecutable = false;
param.GenerateInMemory = true;
// Compile the source
var result = provider.CompileAssemblyFromSource(param, source);
if (result.Errors.Count > 0)
{
var msg = new StringBuilder();
foreach (CompilerError error in result.Errors)
{
msg.AppendFormat("Error ({0}): {1}\n",
error.ErrorNumber, error.ErrorText);
}
throw new Exception(msg.ToString());
}
// Return the assembly
return result.CompiledAssembly;
}
}
Attached below is the error log.
PlayerConnection initialized from D:/Documents/Unity/MRS_Main/build/MRS_Main_Data (debug = 0)
PlayerConnection initialized network socket : 0.0.0.0 55266
Multi-casting "[IP] Hide [Port] 55266 [Flags] 3 [Guid] 1841677575 [EditorId] 2013053427 [Version] 1048832 [Id] WindowsPlayer(DESKTOP-Hide) [Debug] 1 [PackageName] WindowsPlayer [ProjectName] MRS_Main" to [225.0.0.222:54997]...
Waiting for connection from host on [0.0.0.0:55266]...
Remaining time:8s
Remaining time:7s
Remaining time:6s
Remaining time:5s
Remaining time:4s
Remaining time:3s
Remaining time:2s
Remaining time:1s
Remaining time:0s
Timed out. Continuing without host connection.
Started listening to [0.0.0.0:55266]
Starting managed debugger on port 56575
PlayerConnection already initialized - listening to [0.0.0.0:55266]
Initialize engine version: 2019.3.15f1 (59ff3e03856d)
[Subsystems] Discovering subsystems at path D:/Documents/Unity/MRS_Main/build/MRS_Main_Data/UnitySubsystems
GfxDevice: creating device client; threaded=1
Direct3D:
Version: Direct3D 11.0 [level 11.1]
Renderer: NVIDIA GeForce RTX 3050 Laptop GPU (ID=0x25a2)
Vendor:
VRAM: 3991 MB
Driver: 30.0.14.9649
D3D11 device created for Microsoft Media Foundation video decoding.
<RI> Initializing input.
<RI> Input initialized.
<RI> Initialized touch support.
UnloadTime: 0.496700 ms
Curl error 7: Failed to connect to 127.0.0.1 port 8800: Connection refused
(Filename: C:\buildslave\unity\build\Modules/UnityWebRequest/Implementations/TransportCurl.cpp Line: 813)
NotSupportedException: Microsoft.CSharp.CSharpCodeProvider::.ctor
at Microsoft.CSharp.CSharpCodeProvider..ctor () [0x00000] in <00000000000000000000000000000000>:0
at RobotMove.Compile (System.String source) [0x00000] in <00000000000000000000000000000000>:0
at RobotMove+<caserun>d__10.MoveNext () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.MonoBehaviour.StartCoroutine (System.Collections.IEnumerator routine) [0x00000] in <00000000000000000000000000000000>:0
at RobotMove+<caserunner>d__8.MoveNext () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.MonoBehaviour.StartCoroutine (System.Collections.IEnumerator routine) [0x00000] in <00000000000000000000000000000000>:0
at RobotUI.StartOnClick () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ThreadStart.Invoke () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.Events.InvokableCall.Invoke () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.Events.UnityEvent.Invoke () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.UI.Button.Press () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) [0x00000] in <00000000000000000000000000000000>:0
at System.EventHandler`1[TEventArgs].Invoke (System.Object sender, TEventArgs e) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.StandaloneInputModule.ReleaseMouse (UnityEngine.EventSystems.PointerEventData pointerEvent, UnityEngine.GameObject currentOverGo) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.StandaloneInputModule.ProcessMousePress (UnityEngine.EventSystems.PointerInputModule+MouseButtonEventData data) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.StandaloneInputModule.ProcessMouseEvent (System.Int32 id) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.StandaloneInputModule.ProcessMouseEvent () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.StandaloneInputModule.Process () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.EventSystem.Update () [0x00000] in <00000000000000000000000000000000>:0
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
<caserunner>d__8:MoveNext()
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
RobotUI:StartOnClick()
System.Threading.ThreadStart:Invoke()
UnityEngine.Events.InvokableCall:Invoke()
UnityEngine.Events.UnityEvent:Invoke()
UnityEngine.UI.Button:Press()
UnityEngine.UI.Button:OnPointerClick(PointerEventData)
UnityEngine.EventSystems.ExecuteEvents:Execute(IPointerClickHandler, BaseEventData)
System.EventHandler`1:Invoke(Object, TEventArgs)
UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
UnityEngine.EventSystems.StandaloneInputModule:ReleaseMouse(PointerEventData, GameObject)
UnityEngine.EventSystems.StandaloneInputModule:ProcessMousePress(MouseButtonEventData)
UnityEngine.EventSystems.StandaloneInputModule:ProcessMouseEvent(Int32)
UnityEngine.EventSystems.StandaloneInputModule:ProcessMouseEvent()
UnityEngine.EventSystems.StandaloneInputModule:Process()
UnityEngine.EventSystems.EventSystem:Update()
(Filename: currently not available on il2cpp Line: -1)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613061412im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
error compiling c# 1 Answer
Multiple Cars not working 1 Answer
compile error with this script 1 Answer
Vector3 multiplied by its magnitude results in infinity? 1 Answer