Files
ThingsGateway/src/Admin/ThingsGateway.Furion/EventBus/Contexts/EventHandlerContext.cs
2025-05-14 18:52:19 +08:00

88 lines
2.6 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.

// ------------------------------------------------------------------------
// 版权信息
// 版权归百小僧及百签科技(广东)有限公司所有。
// 所有权利保留。
// 官方网站https://baiqian.com
//
// 许可证信息
// 项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。
// 许可证的完整文本可以在源代码树根目录中的 LICENSE-APACHE 和 LICENSE-MIT 文件中找到。
// ------------------------------------------------------------------------
using System.Reflection;
using System.Text.Json;
namespace ThingsGateway.EventBus;
/// <summary>
/// 事件处理程序上下文
/// </summary>
public abstract class EventHandlerContext
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="eventSource">事件源(事件承载对象)</param>
/// <param name="properties">共享上下文数据</param>
/// <param name="handlerMethod">触发的方法</param>
/// <param name="attribute">订阅特性</param>
internal EventHandlerContext(IEventSource eventSource
, IDictionary<object, object> properties
, MethodInfo handlerMethod
, EventSubscribeAttribute attribute)
{
Source = eventSource;
Properties = properties;
HandlerMethod = handlerMethod;
Attribute = attribute;
}
/// <summary>
/// 事件源(事件承载对象)
/// </summary>
public IEventSource Source { get; }
/// <summary>
/// 共享上下文数据
/// </summary>
public IDictionary<object, object> Properties { get; set; }
/// <summary>
/// 触发的方法
/// </summary>
/// <remarks>如果是动态订阅,可能为 null</remarks>
public MethodInfo HandlerMethod { get; }
/// <summary>
/// 订阅特性
/// </summary>
/// <remarks><remarks>如果是动态订阅,可能为 null</remarks></remarks>
public EventSubscribeAttribute Attribute { get; }
private static JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions(JsonSerializerOptions.Default)
{
PropertyNameCaseInsensitive = true
};
/// <summary>
/// 获取负载数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T GetPayload<T>()
{
var rawPayload = Source.Payload;
if (rawPayload is null)
{
return default;
}
else if (rawPayload is JsonElement jsonElement)
{
return JsonSerializer.Deserialize<T>(jsonElement.GetRawText(), JsonSerializerOptions);
}
else
{
return (T)rawPayload;
}
}
}