/
CustomAttributeData.cs
92 lines (73 loc) · 2.93 KB
/
CustomAttributeData.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
87
88
89
90
91
92
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
namespace System.Reflection
{
public class CustomAttributeData
{
#region Public Static Members
public static IList<CustomAttributeData> GetCustomAttributes(MemberInfo target)
{
ArgumentNullException.ThrowIfNull(target);
return target.GetCustomAttributesData();
}
public static IList<CustomAttributeData> GetCustomAttributes(Module target)
{
ArgumentNullException.ThrowIfNull(target);
return target.GetCustomAttributesData();
}
public static IList<CustomAttributeData> GetCustomAttributes(Assembly target)
{
ArgumentNullException.ThrowIfNull(target);
return target.GetCustomAttributesData();
}
public static IList<CustomAttributeData> GetCustomAttributes(ParameterInfo target)
{
ArgumentNullException.ThrowIfNull(target);
return target.GetCustomAttributesData();
}
#endregion
protected CustomAttributeData()
{
}
#region Object Override
public override string ToString()
{
var vsb = new ValueStringBuilder(stackalloc char[256]);
vsb.Append('[');
vsb.Append(Constructor.DeclaringType!.FullName);
vsb.Append('(');
bool first = true;
IList<CustomAttributeTypedArgument> constructorArguments = ConstructorArguments;
int constructorArgumentsCount = constructorArguments.Count;
for (int i = 0; i < constructorArgumentsCount; i++)
{
if (!first) vsb.Append(", ");
vsb.Append(constructorArguments[i].ToString());
first = false;
}
IList<CustomAttributeNamedArgument> namedArguments = NamedArguments;
int namedArgumentsCount = namedArguments.Count;
for (int i = 0; i < namedArgumentsCount; i++)
{
if (!first) vsb.Append(", ");
vsb.Append(namedArguments[i].ToString());
first = false;
}
vsb.Append(")]");
return vsb.ToString();
}
public override int GetHashCode() => base.GetHashCode();
public override bool Equals(object? obj) => obj == (object)this;
#endregion
#region Public Members
public virtual Type AttributeType => Constructor.DeclaringType!;
// Expected to be overridden
public virtual ConstructorInfo Constructor => null!;
public virtual IList<CustomAttributeTypedArgument> ConstructorArguments => null!;
public virtual IList<CustomAttributeNamedArgument> NamedArguments => null!;
#endregion
}
}