Skip to content

Commit d2b107f

Browse files
Copilotjongalloway
andcommitted
Improve type safety: Replace List<object> with strongly-typed records
Define SdkInfo and RuntimeInfo records for better type safety and maintainability. This eliminates the need for reflection to access properties and makes the code more robust. - Added SdkInfo record with Version and Path properties - Added RuntimeInfo record with Name, Version, and Path properties - Updated GetSdkInfo to use List<SdkInfo> instead of List<object> - Updated GetRuntimeInfo to use List<RuntimeInfo> instead of List<object> - Simplified latestSdk access by directly using record property instead of reflection Co-authored-by: jongalloway <[email protected]>
1 parent a959c5d commit d2b107f

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

DotNetMcp/DotNetResources.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44

55
namespace DotNetMcp;
66

7+
/// <summary>
8+
/// Represents SDK information.
9+
/// </summary>
10+
internal record SdkInfo(string Version, string Path);
11+
12+
/// <summary>
13+
/// Represents runtime information.
14+
/// </summary>
15+
internal record RuntimeInfo(string Name, string Version, string Path);
16+
717
/// <summary>
818
/// MCP Resources for .NET environment information.
919
/// Provides read-only access to .NET SDK, runtime, template, and framework metadata.
@@ -31,7 +41,7 @@ public async Task<string> GetSdkInfo()
3141
var result = await DotNetCommandExecutor.ExecuteCommandForResourceAsync("--list-sdks", _logger);
3242

3343
// Parse the SDK list output
34-
var sdks = new List<object>();
44+
var sdks = new List<SdkInfo>();
3545
var lines = result.Split('\n', StringSplitOptions.RemoveEmptyEntries);
3646

3747
foreach (var line in lines)
@@ -42,15 +52,14 @@ public async Task<string> GetSdkInfo()
4252
{
4353
var version = parts[0].Trim();
4454
var path = parts[1].TrimEnd(']').Trim();
45-
sdks.Add(new { version, path = System.IO.Path.Combine(path, version) });
55+
sdks.Add(new SdkInfo(version, System.IO.Path.Combine(path, version)));
4656
}
4757
}
4858

49-
var lastSdk = sdks.Count > 0 ? sdks[sdks.Count - 1] : null;
5059
var response = new
5160
{
5261
sdks,
53-
latestSdk = lastSdk?.GetType().GetProperty("version")?.GetValue(lastSdk) as string
62+
latestSdk = sdks.Count > 0 ? sdks[sdks.Count - 1].Version : null
5463
};
5564

5665
return JsonSerializer.Serialize(response, new JsonSerializerOptions { WriteIndented = true });
@@ -75,7 +84,7 @@ public async Task<string> GetRuntimeInfo()
7584
var result = await DotNetCommandExecutor.ExecuteCommandForResourceAsync("--list-runtimes", _logger);
7685

7786
// Parse the runtime list output
78-
var runtimes = new List<object>();
87+
var runtimes = new List<RuntimeInfo>();
7988
var lines = result.Split('\n', StringSplitOptions.RemoveEmptyEntries);
8089

8190
foreach (var line in lines)
@@ -90,7 +99,7 @@ public async Task<string> GetRuntimeInfo()
9099
var name = nameAndVersion[0];
91100
var version = nameAndVersion[1];
92101
var path = parts[1].TrimEnd(']').Trim();
93-
runtimes.Add(new { name, version, path = System.IO.Path.Combine(path, version) });
102+
runtimes.Add(new RuntimeInfo(name, version, System.IO.Path.Combine(path, version)));
94103
}
95104
}
96105
}

0 commit comments

Comments
 (0)