fury icon indicating copy to clipboard operation
fury copied to clipboard

[C#] Support Dictionary Comparer/Comparator Serialization

Open chaokunyang opened this issue 9 months ago • 6 comments

Feature Request

Support Dictionary Comparer/Comparator Serialization.

Is your feature request related to a problem? Please describe

No response

Describe the solution you'd like

Support Dictionary Comparer/Comparator Serialization can be implemented, but if it's a delegate, how to serialize delegate is still an issue

Describe alternatives you've considered

No response

Additional context

No response

chaokunyang avatar Feb 23 '26 14:02 chaokunyang

@chaokunyang Please assign this to me.

miantalha45 avatar Feb 23 '26 16:02 miantalha45

Hi @miantalha45 , I think it's better to finish exising taken issues before taking more so we can move faster.

chaokunyang avatar Feb 23 '26 16:02 chaokunyang

Hi @chaokunyang I only have a single assigned issue on which i am working and ready to submit a PR. For all other assigned issue, i have created PRs that are under review.

miantalha45 avatar Feb 23 '26 16:02 miantalha45

@miantalha45 Assigned to you. But this is a little complicated, the Comparer/Comparator is a delegate and I'm not sure whether there is a reliable approach to serialize it. You may need to do some research.

chaokunyang avatar Feb 23 '26 16:02 chaokunyang

ok @chaokunyang I will start researching how (and if) this kind of delegate-based comparator can be reliably serialized, and look into approaches used in similar cases. I will update here once I have more clarity or a potential direction.

miantalha45 avatar Feb 23 '26 16:02 miantalha45

Hi @chaokunyang

I looked into the delegate concern around Dictionary<K,V>.Comparer. It turns out it’s not a delegate, it’s an IEqualityComparer (same for SortedDictionary, which uses IComparer). So we are dealing with regular objects, not function pointers.

My thinking is to handle it in a few simple cases:

If it is the default comparer (EqualityComparer.Default), we don’t serialize anything extra. Just write a small flag and reconstruct normally.

For well-known StringComparer singletons (e.g., Ordinal, OrdinalIgnoreCase), we can write a flag + small ID and recreate them on read.

For custom comparer classes, we serialize them as normal objects via Fory’s type resolver (assuming the type is registered).

If the comparer is compiler-generated or wraps a delegate, we reject it with a clear NotSupportedException.

In cross-language mode, I would skip comparer serialization entirely since it’s runtime-specific.

Let me know if this sounds reasonable or if you would prefer limiting support further.

miantalha45 avatar Feb 28 '26 07:02 miantalha45