Search code examples
c#typescriptindex-signature

Create model in C# for TS Index Signatures object


My angular fronend sending to .net core contoler object with index signature object. Something like

export interface LazyLoadEvent {
    first?: number;
    rows?: number;
    filters?: {
        [s: string]: FilterMetadata;
    };
    
}
export interface FilterMetadata {
    value?: any;
    matchMode?: string;
    operator?: string;
}

I am trying create models in C# for request like this. I am confused how I construct property filter in C# ? What would be the equivalent of TS Index Signatures implantation in C# ?

From typescriptlang Index Signatures Sometimes you don’t know all the names of a type’s properties ahead of time, but you do know the shape of the values. In those cases you can use an index signature to describe the types of possible values


Solution

  • You could use a Dictionary

    Dictionary<string, FilterMetadata> Filters { get; set; }
    

    Which you can access using the indexer access

    var value = Filters["SomeKey"];
    

    or by using the TryGetValue method.