Skip to content

Commit 7235609

Browse files
authored
Use lazy property initialization in C# RPC classes (github#725)
Switched property initializations to lazy accessors for lists, dictionaries, and custom types in C# RPC classes. Updated codegen in csharp.ts to emit these accessors, improving memory usage and consistency.
1 parent 2b3337a commit 7235609

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

dotnet/src/Generated/Rpc.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ public class ModelCapabilities
6868
{
6969
/// <summary>Gets or sets the <c>supports</c> value.</summary>
7070
[JsonPropertyName("supports")]
71-
public ModelCapabilitiesSupports Supports { get; set; } = new();
71+
public ModelCapabilitiesSupports Supports { get => field ??= new(); set; }
7272

7373
/// <summary>Gets or sets the <c>limits</c> value.</summary>
7474
[JsonPropertyName("limits")]
75-
public ModelCapabilitiesLimits Limits { get; set; } = new();
75+
public ModelCapabilitiesLimits Limits { get => field ??= new(); set; }
7676
}
7777

7878
/// <summary>Policy state (if applicable).</summary>
@@ -108,7 +108,7 @@ public class Model
108108

109109
/// <summary>Model capabilities and limits.</summary>
110110
[JsonPropertyName("capabilities")]
111-
public ModelCapabilities Capabilities { get; set; } = new();
111+
public ModelCapabilities Capabilities { get => field ??= new(); set; }
112112

113113
/// <summary>Policy state (if applicable).</summary>
114114
[JsonPropertyName("policy")]
@@ -132,7 +132,7 @@ public class ModelsListResult
132132
{
133133
/// <summary>List of available models with full metadata.</summary>
134134
[JsonPropertyName("models")]
135-
public List<Model> Models { get; set; } = [];
135+
public List<Model> Models { get => field ??= []; set; }
136136
}
137137

138138
/// <summary>RPC data type for Tool operations.</summary>
@@ -164,7 +164,7 @@ public class ToolsListResult
164164
{
165165
/// <summary>List of available built-in tools with metadata.</summary>
166166
[JsonPropertyName("tools")]
167-
public List<Tool> Tools { get; set; } = [];
167+
public List<Tool> Tools { get => field ??= []; set; }
168168
}
169169

170170
/// <summary>RPC data type for ToolsList operations.</summary>
@@ -208,7 +208,7 @@ public class AccountGetQuotaResult
208208
{
209209
/// <summary>Quota snapshots keyed by type (e.g., chat, completions, premium_interactions).</summary>
210210
[JsonPropertyName("quotaSnapshots")]
211-
public Dictionary<string, AccountGetQuotaResultQuotaSnapshotsValue> QuotaSnapshots { get; set; } = [];
211+
public Dictionary<string, AccountGetQuotaResultQuotaSnapshotsValue> QuotaSnapshots { get => field ??= []; set; }
212212
}
213213

214214
/// <summary>RPC data type for SessionLog operations.</summary>
@@ -374,7 +374,7 @@ public class SessionWorkspaceListFilesResult
374374
{
375375
/// <summary>Relative file paths in the workspace files directory.</summary>
376376
[JsonPropertyName("files")]
377-
public List<string> Files { get; set; } = [];
377+
public List<string> Files { get => field ??= []; set; }
378378
}
379379

380380
/// <summary>RPC data type for SessionWorkspaceListFiles operations.</summary>
@@ -467,7 +467,7 @@ public class SessionAgentListResult
467467
{
468468
/// <summary>Available custom agents.</summary>
469469
[JsonPropertyName("agents")]
470-
public List<Agent> Agents { get; set; } = [];
470+
public List<Agent> Agents { get => field ??= []; set; }
471471
}
472472

473473
/// <summary>RPC data type for SessionAgentList operations.</summary>
@@ -531,7 +531,7 @@ public class SessionAgentSelectResult
531531
{
532532
/// <summary>The newly selected custom agent.</summary>
533533
[JsonPropertyName("agent")]
534-
public SessionAgentSelectResultAgent Agent { get; set; } = new();
534+
public SessionAgentSelectResultAgent Agent { get => field ??= new(); set; }
535535
}
536536

537537
/// <summary>RPC data type for SessionAgentSelect operations.</summary>

scripts/codegen/csharp.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -662,13 +662,17 @@ function emitRpcClass(className: string, schema: JSONSchema7, visibility: "publi
662662
lines.push(` [JsonPropertyName("${propName}")]`);
663663

664664
let defaultVal = "";
665+
let propAccessors = "{ get; set; }";
665666
if (isReq && !csharpType.endsWith("?")) {
666667
if (csharpType === "string") defaultVal = " = string.Empty;";
667668
else if (csharpType === "object") defaultVal = " = null!;";
668-
else if (csharpType.startsWith("List<") || csharpType.startsWith("Dictionary<")) defaultVal = " = [];";
669-
else if (emittedRpcClasses.has(csharpType)) defaultVal = " = new();";
669+
else if (csharpType.startsWith("List<") || csharpType.startsWith("Dictionary<")) {
670+
propAccessors = "{ get => field ??= []; set; }";
671+
} else if (emittedRpcClasses.has(csharpType)) {
672+
propAccessors = "{ get => field ??= new(); set; }";
673+
}
670674
}
671-
lines.push(` public ${csharpType} ${csharpName} { get; set; }${defaultVal}`);
675+
lines.push(` public ${csharpType} ${csharpName} ${propAccessors}${defaultVal}`);
672676
if (i < props.length - 1) lines.push("");
673677
}
674678
lines.push(`}`);

0 commit comments

Comments
 (0)