This is built-in and can be generated through 'SourceGenerator'.
[Union(0, typeof(Test1))]
[Union(1, typeof(Test2))]
public abstract class Test<T>
{
[Key(0)]
public Dictionary<string, T> Children { get; set; }
}
[MessagePackObject]
public class Test1 : Test<string>
{
}
[MessagePackObject]
public class Test2 : Test<Color>
{
}
Unfortunately, this approach does not have the capability to generate code.
[Union(0, typeof(Test1))]
[Union(1, typeof(Test2))]
public abstract class Test<T>
{
[Key(0)]
[MessagePackFormatter(typeof(CustomDictionaryFormatter<,>))] //Not working
[MessagePackFormatter(typeof(CustomDictionaryFormatter<string,T>))] //Not working
public CustomDictionary<string, T> Children { get; set; }
}
[MessagePackObject]
public class Test1 : Test<string>
{
}
[MessagePackObject]
public class Test2 : Test<Color>
{
}
public sealed class CustomDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, CustomDictionary<TKey, TValue>, CustomDictionary<TKey, TValue>.Enumerator, CustomDictionary<TKey, TValue>> where TKey : notnull
{
protected override void Add(CustomDictionary<TKey, TValue> collection, int index, TKey key, TValue value, MessagePackSerializerOptions options)
{
collection.Add(key, value);
}
protected override CustomDictionary<TKey, TValue> Complete(CustomDictionary<TKey, TValue> intermediateCollection)
{
return intermediateCollection;
}
protected override CustomDictionary<TKey, TValue> Create(int count, MessagePackSerializerOptions options)
{
return new CustomDictionary<TKey, TValue>(count, options.Security.GetEqualityComparer<TKey>());
}
protected override CustomDictionary<TKey, TValue>.Enumerator GetSourceEnumerator(CustomDictionary<TKey, TValue> source)
{
return source.GetEnumerator();
}
}
While it is the only way to generate the code correctly, it is quite inconvenient. I was wondering if there are any better alternatives available?
[Union(0, typeof(Test1))]
[Union(1, typeof(Test2))]
public abstract class Test<T>
{
[IgnoreMember]
public abstract CustomDictionary<string, T> Children { get; set; }
}
[MessagePackObject]
public class Test1 : Test<string>
{
[Key(0)]
[MessagePackFormatter(typeof(CustomDictionaryFormatter<string, string>))]
public override CustomDictionary<string, string> Children { get; set; }
}
[MessagePackObject]
public class Test2 : Test<Color>
{
[Key(0)]
[MessagePackFormatter(typeof(CustomDictionaryFormatter<string, Color>))]
public override CustomDictionary<string, Color> Children { get; set; }
}
public sealed class CustomDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, CustomDictionary<TKey, TValue>, CustomDictionary<TKey, TValue>.Enumerator, CustomDictionary<TKey, TValue>> where TKey : notnull
{
protected override void Add(CustomDictionary<TKey, TValue> collection, int index, TKey key, TValue value, MessagePackSerializerOptions options)
{
collection.Add(key, value);
}
protected override CustomDictionary<TKey, TValue> Complete(CustomDictionary<TKey, TValue> intermediateCollection)
{
return intermediateCollection;
}
protected override CustomDictionary<TKey, TValue> Create(int count, MessagePackSerializerOptions options)
{
return new CustomDictionary<TKey, TValue>(count, options.Security.GetEqualityComparer<TKey>());
}
protected override CustomDictionary<TKey, TValue>.Enumerator GetSourceEnumerator(CustomDictionary<TKey, TValue> source)
{
return source.GetEnumerator();
}
}
Thank you very much for your patience and attention. I would greatly appreciate any suggestions or alternative methods you may have.
This is built-in and can be generated through 'SourceGenerator'.
Unfortunately, this approach does not have the capability to generate code.
While it is the only way to generate the code correctly, it is quite inconvenient. I was wondering if there are any better alternatives available?
Thank you very much for your patience and attention. I would greatly appreciate any suggestions or alternative methods you may have.