using System;
|
using System.CodeDom;
|
using System.CodeDom.Compiler;
|
using System.Collections;
|
using System.Collections.Generic;
|
using System.Reflection;
|
|
public class GenerateClass
|
{
|
private string _classPath; //生成脚本路径
|
private CodeNamespace _codeNamespace;//命名空间
|
public CodeTypeDeclaration _customerClass;
|
private CodeCompileUnit _unit;
|
|
public GenerateClass(string classPath)
|
{
|
_classPath = classPath;
|
}
|
|
/// <summary>
|
/// 创建命名空间
|
/// </summary>
|
/// <param name="spaceName"></param>
|
public void GenerateNamespace(string spaceName)
|
{
|
_unit = new CodeCompileUnit();
|
_codeNamespace = new CodeNamespace(spaceName);
|
_unit.Namespaces.Add(_codeNamespace);
|
}
|
|
/// <summary>
|
/// 添加引入的命名空间
|
/// </summary>
|
/// <param name="spaceName"></param>
|
public void AddImportNamespace(string spaceName)
|
{
|
if (_codeNamespace == null) {
|
DesignDebug.Log("_codeNamespace is null");
|
return;
|
}
|
_codeNamespace.Imports.Add(new CodeNamespaceImport(spaceName));
|
}
|
|
/// <summary>
|
/// 创建类名
|
/// </summary>
|
/// <param name="className"></param>
|
public void GenerateClassName(string className, string extendClass = "")
|
{
|
if (_codeNamespace == null) {
|
DesignDebug.Log("_codeNamespace is null");
|
return;
|
}
|
_customerClass = new CodeTypeDeclaration(className);
|
_customerClass.IsClass = true;
|
_customerClass.IsPartial = true;
|
_customerClass.TypeAttributes = TypeAttributes.Public;
|
if (!string.IsNullOrEmpty(extendClass)) {
|
_customerClass.BaseTypes.Add(extendClass);
|
}
|
_codeNamespace.Types.Add(_customerClass);
|
}
|
|
/// <summary>
|
/// 添加熟悉
|
/// </summary>
|
/// <param name="type"></param>
|
/// <param name="fieldName"></param>
|
public void AddMemberField(Type type, string srcFieldName, string fieldName, string CommentStatement)
|
{
|
if (type == null) {
|
UnityEngine.Debug.LogError(_customerClass.Name + " " + srcFieldName + "`s type is Null");
|
return;
|
}
|
CodeMemberField field = new CodeMemberField(type, fieldName);
|
field.Attributes = MemberAttributes.Private;
|
_customerClass.Members.Add(field);
|
|
CodeMemberProperty property = new CodeMemberProperty();
|
|
property.Attributes = MemberAttributes.Public | MemberAttributes.Final;
|
|
property.Name = srcFieldName;
|
|
property.HasGet = true;
|
|
property.HasSet = false;
|
|
property.Type = new CodeTypeReference(type);
|
|
property.Comments.Add(new CodeCommentStatement(CommentStatement));
|
|
property.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fieldName)));
|
|
//property.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fieldName), new CodePropertySetValueReferenceExpression()));
|
|
_customerClass.Members.Add(property);
|
}
|
|
/// <summary>
|
/// 添加方法
|
/// </summary>
|
public void AddFunction(string funcName, MemberAttributes memberAtt, List<KeyValuePair<Type, string>> paramLst, List<string> statementLst, Type returanType)
|
{
|
//为这个类添加一个方法 public override int 方法名(string str);
|
System.CodeDom.CodeMemberMethod method = new CodeMemberMethod();
|
method.Name = funcName;
|
method.Attributes = memberAtt;//声明方法是公开 并且override的
|
|
if (paramLst != null) {
|
for (int i = 0; i < paramLst.Count; i++) {
|
method.Parameters.Add(new CodeParameterDeclarationExpression(paramLst[i].Key, paramLst[i].Value));
|
}
|
}
|
|
if (statementLst != null) {
|
for (int i = 0; i < statementLst.Count; i++) {
|
method.Statements.Add(new CodeSnippetStatement(statementLst[i]));
|
}
|
}
|
|
method.ReturnType = new CodeTypeReference(returanType);//声明返回值的类型
|
_customerClass.Members.Add(method);
|
}
|
|
/// <summary>
|
/// 写入文件
|
/// </summary>
|
public void WriteToFile()
|
{
|
_customerClass.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(SerializableAttribute))));
|
|
//生成代码
|
|
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
|
|
CodeGeneratorOptions options = new CodeGeneratorOptions();
|
|
options.BracingStyle = "C";
|
|
options.BlankLinesBetweenMembers = true;
|
|
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(_classPath)) {
|
provider.GenerateCodeFromCompileUnit(_unit, sw, options);
|
}
|
}
|
}
|