-
Notifications
You must be signed in to change notification settings - Fork 2
/
RpcInstance.cs
82 lines (72 loc) · 2.79 KB
/
RpcInstance.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using System.Reflection;
using JieRuntime.Rpc.Attributes;
namespace JieRuntime.Rpc
{
/// <summary>
/// 表示远程调用服务实例的类
/// </summary>
public readonly struct RpcInstance
{
#region --属性--
/// <summary>
/// 获取实例的接口类型
/// </summary>
public Type InstanceType { get; }
/// <summary>
/// 获取接口实例对应的实例对象
/// </summary>
public object Instance { get; }
/// <summary>
/// 获取实例对象的所有方法
/// </summary>
public IReadOnlyDictionary<string, List<MethodInfo>> Methods { get; }
#endregion
#region --构造函数--
/// <summary>
/// 初始化 <see cref="RpcInstance"/> 的新实例
/// </summary>
/// <param name="instanceType">实例的类型</param>
/// <param name="instance">实例对象</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public RpcInstance (Type instanceType, object instance)
{
this.InstanceType = instanceType ?? throw new ArgumentNullException (nameof (instanceType));
this.Instance = instance ?? throw new ArgumentNullException (nameof (instance));
if (!instanceType.IsInterface)
{
throw new ArgumentException ("实例类型必须是接口类型", nameof (instanceType));
}
// instance 必须继承自 instanceType
if (!instanceType.IsAssignableFrom (instance.GetType ()))
{
throw new ArgumentException ($"实例类型必须继承自 {instanceType.FullName}", nameof (instanceType));
}
Dictionary<string, List<MethodInfo>> methods = new ();
foreach (MethodInfo methodInfo in instanceType.GetMethods ())
{
RemoteMethodAttribute remoteMethod = methodInfo.GetCustomAttribute<RemoteMethodAttribute> ();
if (remoteMethod is not null)
{
if (!methods.ContainsKey (remoteMethod.Name))
{
methods.Add (remoteMethod.Name, new List<MethodInfo> ());
}
methods[remoteMethod.Name].Add (methodInfo);
}
else
{
if (!methods.ContainsKey (methodInfo.Name))
{
methods.Add (methodInfo.Name, new List<MethodInfo> ());
}
methods[methodInfo.Name].Add (methodInfo);
}
}
this.Methods = methods;
}
#endregion
}
}