-
Notifications
You must be signed in to change notification settings - Fork 774
Expand file tree
/
Copy pathParameterHelper.cs
More file actions
91 lines (77 loc) · 2.45 KB
/
ParameterHelper.cs
File metadata and controls
91 lines (77 loc) · 2.45 KB
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
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
namespace Python.Runtime.Reflection;
[Serializable]
class ParameterHelper : IEquatable<ParameterInfo>
{
public readonly string TypeName;
public readonly ParameterModifier Modifier;
public readonly ParameterHelper[]? GenericArguments;
public ParameterHelper(ParameterInfo tp) : this(tp.ParameterType)
{
Modifier = ParameterModifier.None;
if (tp.IsIn && tp.ParameterType.IsByRef)
{
Modifier = ParameterModifier.In;
}
else if (tp.IsOut && tp.ParameterType.IsByRef)
{
Modifier = ParameterModifier.Out;
}
else if (tp.ParameterType.IsByRef)
{
Modifier = ParameterModifier.Ref;
}
}
public ParameterHelper(Type type)
{
TypeName = type.AssemblyQualifiedName;
if (TypeName is null)
{
if (type.IsByRef || type.IsArray)
{
TypeName = type.IsArray ? "[]" : "&";
GenericArguments = new[] { new ParameterHelper(type.GetElementType()) };
}
else
{
Debug.Assert(type.ContainsGenericParameters);
TypeName = $"{type.Assembly}::{type.Namespace}/{type.Name}";
GenericArguments = type.GenericTypeArguments.Select(t => new ParameterHelper(t)).ToArray();
}
}
}
public bool IsSpecialType => TypeName == "&" || TypeName == "[]";
public bool Equals(ParameterInfo other)
{
return this.Equals(new ParameterHelper(other));
}
public bool Matches(ParameterInfo other) => this.Equals(other);
public bool Equals(ParameterHelper other)
{
if (other is null) return false;
if (!(other.TypeName == TypeName && other.Modifier == Modifier))
return false;
if (GenericArguments == other.GenericArguments) return true;
if (GenericArguments is not null && other.GenericArguments is not null)
{
if (GenericArguments.Length != other.GenericArguments.Length) return false;
for (int arg = 0; arg < GenericArguments.Length; arg++)
{
if (!GenericArguments[arg].Equals(other.GenericArguments[arg])) return false;
}
return true;
}
return false;
}
}
enum ParameterModifier
{
None,
In,
Out,
Ref
}