Skip to content

Commit d643ece

Browse files
committed
feat(ProtoBuffUnityHelper): 为有父类的操作码添加Clear方法实现
为继承父类的操作码类型生成Clear方法,该方法会清空所有字段值或集合内容,确保对象可被正确重置
1 parent f36ecd6 commit d643ece

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

ProtoExport/ProtoBuffUnityHelper.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,40 @@ public void Run(MessageInfoList messageInfoList, string outputPath, string names
9292
sb.AppendLine();
9393
}
9494

95+
96+
if (!string.IsNullOrEmpty(operationCodeInfo.ParentClass))
97+
{
98+
sb.AppendLine();
99+
sb.AppendLine("\t\tpublic override void Clear()");
100+
sb.AppendLine("\t\t{");
101+
for (var index = 0; index < operationCodeInfo.Fields.Count; index++)
102+
{
103+
var operationField = operationCodeInfo.Fields[index];
104+
if (!operationField.IsValid)
105+
{
106+
continue;
107+
}
108+
109+
if (operationField.IsRepeated)
110+
{
111+
sb.AppendLine($"\t\t\t{operationField.Name}.Clear();");
112+
}
113+
else
114+
{
115+
if (operationField.IsKv)
116+
{
117+
sb.AppendLine($"\t\t\t{operationField.Name}.Clear();");
118+
}
119+
else
120+
{
121+
sb.AppendLine($"\t\t\t{operationField.Name} = default;");
122+
}
123+
}
124+
}
125+
126+
sb.AppendLine("\t\t}");
127+
}
128+
95129
sb.AppendLine("\t}");
96130
sb.AppendLine();
97131
}

0 commit comments

Comments
 (0)