//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
// CSDN博客:https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频:https://space.bilibili.com/94253567
// Gitee源代码仓库:https://gitee.com/RRQM_Home
// Github源代码仓库:https://github.com/RRQM
// API首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using TouchSocket.Core;
namespace TouchSocket.Rpc
{
///
/// Rpc函数实例
///
public class MethodInstance : Method
{
private RpcAttribute[] m_rpcAttributes;
private RpcAttribute[] m_serverRpcAttributes;
///
/// 构造函数
///
///
public MethodInstance(MethodInfo methodInfo) : base(methodInfo)
{
}
///
/// 描述属性
///
public string GetDescription()
{
return this.Info.GetCustomAttribute()?.Description;
}
///
/// 筛选器
///
public IRpcActionFilter[] Filters { get; internal set; }
///
/// 是否可用
///
public bool IsEnable { get; set; }
///
/// 是否为单例
///
public bool IsSingleton { get; internal set; }
///
/// 函数标识
///
public MethodFlags MethodFlags { get; internal set; }
///
/// 参数名集合
///
public string[] ParameterNames { get; internal set; }
///
/// 参数集合
///
public ParameterInfo[] Parameters { get; internal set; }
///
/// 参数类型集合,已处理out及ref,无参数时为空集合,
///
public Type[] ParameterTypes { get; internal set; }
///
/// Rpc属性集合
///
public RpcAttribute[] RpcAttributes
{
get
{
m_rpcAttributes ??= this.Info.GetCustomAttributes(true).ToArray();
return this.m_rpcAttributes;
}
}
///
/// 服务实例工厂
///
public IRpcServerFactory ServerFactory { get; internal set; }
///
/// Rpc服务属性集合
///
public RpcAttribute[] ServerRpcAttributes
{
get
{
m_serverRpcAttributes ??= this.ServerType.GetCustomAttributes(true).ToArray();
return this.m_serverRpcAttributes;
}
}
///
/// 实例类型
///
public Type ServerType { get; internal set; }
///
/// 获取指定类型属性标签
///
///
///
public T GetAttribute()
{
object attribute = this.GetAttribute(typeof(T));
if (attribute != null)
{
return (T)attribute;
}
return default;
}
///
/// 获取指定类型属性标签
///
///
///
public object GetAttribute(Type attributeType)
{
object attribute = RpcAttributes.FirstOrDefault((a) => { return attributeType.IsAssignableFrom(a.GetType()); });
if (attribute != null)
{
return attribute;
}
attribute = ServerRpcAttributes.FirstOrDefault((a) => { return attributeType.IsAssignableFrom(a.GetType()); });
if (attribute != null)
{
return attribute;
}
return default;
}
}
}