You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

JsonPropertyMap.cs 1.2 kB

1234567891011121314151617181920212223242526272829303132333435
  1. using System;
  2. using System.Reflection;
  3. using System.Text.Json;
  4. namespace Discord.Serialization.Json
  5. {
  6. internal class JsonPropertyMap<TModel, TType> : PropertyMap, IJsonPropertyMap<TModel>
  7. {
  8. private readonly IJsonPropertyConverter<TType> _converter;
  9. private readonly Func<TModel, TType> _getFunc;
  10. private readonly Action<TModel, TType> _setFunc;
  11. public JsonPropertyMap(PropertyInfo propInfo, IJsonPropertyConverter<TType> converter)
  12. : base(propInfo)
  13. {
  14. _converter = converter;
  15. _getFunc = propInfo.GetMethod.CreateDelegate(typeof(Func<TModel, TType>)) as Func<TModel, TType>;
  16. _setFunc = propInfo.SetMethod.CreateDelegate(typeof(Action<TModel, TType>)) as Action<TModel, TType>;
  17. }
  18. public void Write(TModel model, ref JsonWriter writer)
  19. {
  20. var value = _getFunc(model);
  21. if (value == null && ExcludeNull)
  22. return;
  23. _converter.Write(this, ref writer, value, true);
  24. }
  25. public void Read(TModel model, ref JsonReader reader)
  26. {
  27. var value = _converter.Read(this, ref reader, true);
  28. _setFunc(model, value);
  29. }
  30. }
  31. }