public void GenCS(IDataReadable reader) { string className = reader.currentDataTypeName; CodeGener protobufBaseGener = new CodeGener(ProtobufData.nameSpace, className); protobufBaseGener.newClass.AddMemberCostomAttribute("ProtoBuf.ProtoContract"); protobufBaseGener.AddImport("System", "ProtoBuf"); List <string> comment = reader.GetComment(); protobufBaseGener .AddBaseType("ProtobufData<" + className + ">") .AddMemberField(typeof(string), "fileName", (member) => { member.AddFieldMemberInit("\"" + className + "\""); }, System.CodeDom.MemberAttributes.Static | System.CodeDom.MemberAttributes.Final | System.CodeDom.MemberAttributes.Public); reader.GetTitle().ForEach((i, title) => { string[] titleSplit = title.Split('|'); string varName = titleSplit[0]; string typeName = titleSplit[1]; protobufBaseGener.AddMemberProperty(typeName.GetTypeByString(), varName, (member) => { member.AddComment(comment[i], true); member.AddMemberCostomAttribute("ProtoBuf.ProtoMember", (i + 1).ToString()); }); }); PathEx.MakeFileDirectoryExist(PathConfig.GetLoaclGameDataClassPath(PathConfig.DataType.Protobuf)); protobufBaseGener.GenCSharp(PathConfig.GetLoaclGameDataClassPath(PathConfig.DataType.Protobuf)); }
private void GenScriptableObject(List <FieldInfo> list, ScriptComponentLoader scrLoader, Component comp) { CodeGener gener = new CodeGener("ResetCore.ReAssembly.Data", GetScrObjName(comp)); gener .AddImport("UnityEngine") .AddBaseType("ScriptableObject"); foreach (FieldInfo info in list) { gener.AddMemberField(info.FieldType, info.Name, null, System.CodeDom.MemberAttributes.Public); } gener.GenCSharp(DllManagerConst.scriptableCSOutputPath); }
/// <summary> /// 生成序列化 /// </summary> /// <param name="type"></param> public static void GenSerializer(Type type) { if (type == null) { return; } var typeName = type.FullName; var finalName = GetSerializerName(type); CodeGener g = new CodeGener(serializerNamespace, finalName); g.AddBaseType("IXmlSerializer<" + type.FullName + ">"); g.AddImport("System.Xml"); //ToXElement方法 CodeMemberMethod toXElementMethod = new CodeMemberMethod(); toXElementMethod.Name = "Write"; toXElementMethod.Attributes = MemberAttributes.Public; toXElementMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(XmlElement)), "xElement")); toXElementMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(string)), "name")); toXElementMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeName), "obj")); //函数体 toXElementMethod.Statements.AddRange(GetToXElementCodeStatements(type)); g.newClass.Members.Add(toXElementMethod); //Parse方法 CodeMemberMethod parseMethod = new CodeMemberMethod(); parseMethod.Name = "Read"; parseMethod.Attributes = MemberAttributes.Public; parseMethod.ReturnType = new CodeTypeReference(type); parseMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(XmlElement)), "xElement")); parseMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(string)), "name")); //函数体 parseMethod.Statements.AddRange(GetParseXElementCodeStatements(type)); g.newClass.Members.Add(parseMethod); //生成代码 g.GenCSharp(SerializerGenerPath); }