Yezi 133fef14d7
Update DataRequest Setter (#28)
* Update DataRequest Setter

* Fixed some json bug (#29)

* JsonManager应是internal的

---------

Co-authored-by: milimoe <110188673+milimoe@users.noreply.github.com>
2023-06-05 00:31:45 +08:00

54 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Text.Json.Serialization;
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Service;
namespace Milimoe.FunGame.Core.Library.Common.Network
{
[Serializable]
public readonly struct SocketObject
{
public SocketMessageType SocketType { get; } = SocketMessageType.Unknown;
public Guid Token { get; } = Guid.Empty;
public object[] Parameters { get; } = Array.Empty<object>();
public int Length => Parameters.Length;
/// <summary>
/// 从参数列表中获取指定索引的参数的Json字符串
/// -- 此方法仅返回Json字符串对象类型请使用反序列化方法GetParam<T>() --
/// -- 当然也可以自己反序列化 --
/// </summary>
/// <param name="index">索引</param>
/// <returns></returns>
/// <exception cref="IndexOutOfArrayLengthException">索引超过数组上限</exception>
public object? this[int index]
{
get
{
if (index >= Parameters.Length) throw new IndexOutOfArrayLengthException();
object? obj = Parameters[index];
return JsonManager.GetObject(obj.ToString() ?? "");
}
}
[JsonConstructor]
public SocketObject(SocketMessageType SocketType, Guid Token, params object[] Parameters)
{
this.SocketType = SocketType;
this.Token = Token;
if (Parameters != null && Parameters.Length > 0) this.Parameters = Parameters;
}
/// <summary>
/// 从参数列表中获取指定类型和索引的参数
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="index">索引</param>
/// <returns>类型的参数</returns>
/// <exception cref="IndexOutOfArrayLengthException">索引超过数组上限</exception>
public T? GetParam<T>(int index)
{
return JsonManager.GetObject<T>(this, index);
}
}
}