Question by
godfreyidk · Sep 26, 2019 at 07:18 AM ·
compile
Why does codeDom generated class NOT COMPILE
I was able to generate and compile generated classes with unity 2019.1. But with 2019.2 the generated class does not want to compile. It just remains with that orange hue on its tab in the Rider editor.
Here are the important class methods responsible for generating some dynamic class:
private static void CreateBaseGetterClass(string wrappedCompName)
{
CodeDomProvider provider = new CSharpCodeProvider();
CompilerParameters compileParams =
CreateCompilerParams();
string sourceCode = "";
sourceCode += "using UnityEngine;\n";
sourceCode += "using
_Monobehaviours.Abstract_Implementations;\n";
sourceCode += "using
_ScriptableObjects.Implementations.Variables;\n\n";
sourceCode += "namespace
_ScriptableObjects.Abstractions\n";
sourceCode += "{\n\tpublic abstract class " +
_baseGetterClassPrefix +
CreateCompName(wrappedCompName) +
_baseGetterClassNameSuffix + " : " +
$"GetsADataComponent<{
CreateCompName(wrappedCompName)}>\n";
sourceCode += "\t{\n";
sourceCode += $"\t\tpublic override
{CreateCompName(wrappedCompName)}
GetDataComp(ExperiTickerOfStateActions ticker)\n";
sourceCode += "\t\t{\n";
sourceCode += $"\t\t\tif ( !Key.In(ticker.Data,
WantedCompKey) ) return null;\n\n";
sourceCode +=
$"\t\t\t{CreateCompName(wrappedCompName)}
{LowerCaseStartedStr(wrappedCompName)} =
ticker.Data.Components[WantedCompKey.Value.GetType()].
Value as {CreateCompName(wrappedCompName)};\n\n";
sourceCode += $"\t\t\treturn !
{LowerCaseStartedStr(wrappedCompName)} ? null :
{LowerCaseStartedStr(wrappedCompName)};\n";
sourceCode += "\t\t}\n";
sourceCode += "\t}\n";
sourceCode += "}";
WriteAbstractSourceToCsFile(wrappedCompName,
sourceCode, provider, compileParams);
}
private static CompilerParameters CreateCompilerParams()
{
CompilerParameters compileParams = new
CompilerParameters();
compileParams.GenerateInMemory = true;
compileParams.GenerateExecutable = false;
compileParams.ReferencedAssemblies.Add("System.dll");
return compileParams;
}
private static void WriteAbstractSourceToCsFile(string compTypeName, string sourceCodeString, CodeDomProvider provider, CompilerParameters compileParams)
{
string classNamePrefix = _baseGetterClassPrefix +
CreateCompName(compTypeName) +
_baseGetterClassNameSuffix;
string scriptPath = Application.dataPath;
scriptPath += $"/Scripts/_ScriptableObjects/Abstractions/{
classNamePrefix}";
scriptPath += ".cs";
System.IO.File.WriteAllText(scriptPath, sourceCodeString);
provider.CompileAssemblyFromSource(compileParams,
sourceCodeString);
}
And here's a picture of the generated class that remains uncompiled:
Comment
Your answer
Follow this Question
Related Questions
failed to re pack resources 0 Answers
Error at building and executing project. 0 Answers
My playmode is not running there are an error,I write a script to print i use "Debug.Log" 0 Answers
error CS0117: System.Collections.ObjectModel.Collection> does not contain a definition for Items 0 Answers
Alternative to EditorWindow.OnDisable to run before compiling 2 Answers