You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.9 KiB

using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using Microsoft.CSharp;
5 months ago
/* 使CodeCompileUnit需要将Edit->Project Settings->Player->Other Settings->Api Compatibility Level改为.NET 4.x.NET Framework
使
*/
namespace Tools.ExcelResolver.Editor
{
public sealed partial class ExcelResolverEditorWindow
{
private void WriteDataCode(ClassCodeData classCodeData)
{
string outputPath = $"{excelResolverConfig.CodePathRoot}/{classCodeData.className}.cs";
CodeCompileUnit compileUnit = new CodeCompileUnit();
#region 命名空间
CodeNamespace codeNamespace = new CodeNamespace(excelResolverConfig.GenerateDataClassNameSpace);
compileUnit.Namespaces.Add(codeNamespace);
#endregion
#region 引用
string[] classImports = new string[]
{
"System",
"System.Collections",
"System.Collections.Generic",
"UnityEngine",
5 months ago
"Sirenix.OdinInspector",
};
foreach (var import in classImports)
{
codeNamespace.Imports.Add(new CodeNamespaceImport(import));
}
#endregion
#region 类
CodeTypeDeclaration classType = new CodeTypeDeclaration(classCodeData.className)
{
IsClass = true,
TypeAttributes = System.Reflection.TypeAttributes.Public,
// CustomAttributes = new CodeAttributeDeclarationCollection()
// {
// new CodeAttributeDeclaration("Serializable")
// },
BaseTypes =
{
5 months ago
new CodeTypeReference("SerializedScriptableObject"),
new CodeTypeReference("IExcelSOData")
}
};
codeNamespace.Types.Add(classType);
#endregion
#region 字段
foreach (var field in classCodeData.fields.Values)
{
classType.Members.Add(field.GetCodeField());
}
#endregion
#region 代码风格设置
CodeGeneratorOptions options = new CodeGeneratorOptions
{
BracingStyle = "C",
BlankLinesBetweenMembers = true,
VerbatimOrder = true,
};
#endregion
#region 写入文件
using (StreamWriter writer = new StreamWriter(outputPath))
{
CSharpCodeProvider provider = new CSharpCodeProvider();
provider.GenerateCodeFromCompileUnit(compileUnit, writer, options);
}
#endregion
}
}
}