You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中实现TypeScript索引签名对应的请求模型?

TypeScript索引签名在C#中的等价实现方案

TypeScript里带字符串索引签名的对象(比如你代码里的{[s: string]: FilterMetadata}),在C#里最直接的等价实现是用Dictionary<string, FilterMetadata>或者它的接口IDictionary<string, FilterMetadata>,两者都能完美匹配前端传来的键值对格式数据。

给你对应完整的C#模型代码:

首先是对应FilterMetadata的类:

public class FilterMetadata
{
    public object Value { get; set; }
    public string MatchMode { get; set; }
    public string Operator { get; set; }
}

然后是对应LazyLoadEvent的类:

public class LazyLoadEvent
{
    public int? First { get; set; }
    public int? Rows { get; set; }
    public Dictionary<string, FilterMetadata> Filters { get; set; }
}

.NET自带的JSON序列化器(不管是System.Text.Json还是Newtonsoft.Json)都能自动把前端发送的JSON格式数据,反序列化成这个C#模型。实际开发中优先用Dictionary<string, T>就行,它是具体实现类,序列化反序列化过程更顺畅;如果需要依赖抽象的话,再用IDictionary<string, T>。

内容的提问来源于stack exchange,提问作者adopilot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 00:45:35