mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
Fix user option values on .NET 5+
This commit is contained in:
+21
-21
@@ -1,7 +1,10 @@
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Text.Json;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace Kruzya.TelegramBot.Core.Data
|
||||
{
|
||||
@@ -13,30 +16,27 @@ namespace Kruzya.TelegramBot.Core.Data
|
||||
public BotUser BotUser { get; set; }
|
||||
public string Name { get; set; }
|
||||
|
||||
public byte[] Value
|
||||
{
|
||||
get
|
||||
{
|
||||
var fmt = new BinaryFormatter();
|
||||
using var memStream = new MemoryStream();
|
||||
fmt.Serialize(memStream, data);
|
||||
|
||||
return memStream.ToArray();
|
||||
}
|
||||
set
|
||||
{
|
||||
var fmt = new BinaryFormatter();
|
||||
using var memStream = new MemoryStream();
|
||||
memStream.Write(value);
|
||||
memStream.Position = 0;
|
||||
public string ValueType { get; set; }
|
||||
|
||||
data = fmt.Deserialize(memStream);
|
||||
public byte[] Value { get; set; }
|
||||
|
||||
public T GetValue<T>(T defVal = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
return JsonSerializer.Deserialize<T>(new ReadOnlySpan<byte>(Value));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return defVal;
|
||||
}
|
||||
}
|
||||
|
||||
[NonSerialized] public object data;
|
||||
|
||||
public T GetValue<T>() => (T) data;
|
||||
public void SetValue<T>(T value) => data = value;
|
||||
public void SetValue<T>(T value)
|
||||
{
|
||||
Value = JsonSerializer.SerializeToUtf8Bytes(value, new JsonSerializerOptions { WriteIndented = false,
|
||||
IgnoreNullValues = true });
|
||||
ValueType = value.GetType().FullName;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user