-
Notifications
You must be signed in to change notification settings - Fork 87
/
ParameterRef.cs
86 lines (65 loc) · 2.96 KB
/
ParameterRef.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
83
84
85
86
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.VisualStudio.Composition.Reflection
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
[DebuggerDisplay("{" + nameof(DebuggerDisplay) + ",nq}")]
public class ParameterRef : IEquatable<ParameterRef>
{
/// <summary>
/// Gets the string to display in the debugger watch window for this value.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay => $"{this.DeclaringType.FullName}.{this.Method.DebuggerDisplay}(p-index: {this.ParameterIndex})";
/// <summary>
/// A cache behind the <see cref="ParameterInfo"/> property.
/// </summary>
private ParameterInfo cachedParameterInfo;
public ParameterRef(MethodRef method, int parameterIndex)
{
Requires.NotNull(method, nameof(method));
Requires.Range(parameterIndex >= 0, nameof(parameterIndex));
this.Method = method;
this.ParameterIndex = parameterIndex;
}
public ParameterRef(ParameterInfo parameterInfo, Resolver resolver)
{
Requires.NotNull(parameterInfo, nameof(parameterInfo));
Requires.NotNull(resolver, nameof(resolver));
this.Method = new MethodRef((MethodBase)parameterInfo.Member, resolver);
this.ParameterIndex = parameterInfo.Position;
}
public MethodRef Method { get; }
public ParameterInfo ParameterInfo => this.cachedParameterInfo ?? (this.cachedParameterInfo = this.Resolve());
public TypeRef DeclaringType => this.Method.DeclaringType;
public int MethodMetadataToken => this.Method.MetadataToken;
/// <summary>
/// Gets a 0-based index describing which parameter in the method this references.
/// </summary>
public int ParameterIndex { get; }
public AssemblyName AssemblyName => this.DeclaringType.AssemblyName;
internal Resolver Resolver => this.DeclaringType.Resolver;
public static ParameterRef Get(ParameterInfo parameter, Resolver resolver)
{
if (parameter != null)
{
return new ParameterRef(parameter, resolver);
}
return default(ParameterRef);
}
public bool Equals(ParameterRef other)
{
return this.Method.Equals(other.Method)
&& this.ParameterIndex == other.ParameterIndex;
}
public override int GetHashCode() => unchecked(this.Method.MetadataToken + this.ParameterIndex);
public override bool Equals(object obj) => obj is ParameterRef parameter && this.Equals(parameter);
internal void GetInputAssemblies(ISet<AssemblyName> assemblies)
{
Requires.NotNull(assemblies, nameof(assemblies));
this.DeclaringType.GetInputAssemblies(assemblies);
}
}
}