-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathCustomAttributeDataWrapper.cs
51 lines (40 loc) · 2.04 KB
/
CustomAttributeDataWrapper.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
// 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.Linq;
using System.Reflection;
using Microsoft.CodeAnalysis;
namespace System.Text.Json.Reflection
{
internal class CustomAttributeDataWrapper : CustomAttributeData
{
public CustomAttributeDataWrapper(AttributeData a, MetadataLoadContextInternal metadataLoadContext)
{
var namedArguments = new List<CustomAttributeNamedArgument>();
foreach (KeyValuePair<string, TypedConstant> na in a.NamedArguments)
{
var member = a.AttributeClass.BaseTypes().SelectMany(t => t.GetMembers(na.Key)).First();
MemberInfo memberInfo = member is IPropertySymbol
? new PropertyInfoWrapper((IPropertySymbol)member, metadataLoadContext)
: new FieldInfoWrapper((IFieldSymbol)member, metadataLoadContext);
namedArguments.Add(new CustomAttributeNamedArgument(memberInfo, na.Value.Value));
}
var constructorArguments = new List<CustomAttributeTypedArgument>();
foreach (TypedConstant ca in a.ConstructorArguments)
{
if (ca.Kind == TypedConstantKind.Error)
{
continue;
}
object value = ca.Kind == TypedConstantKind.Array ? ca.Values : ca.Value;
constructorArguments.Add(new CustomAttributeTypedArgument(ca.Type.AsType(metadataLoadContext), value));
}
Constructor = new ConstructorInfoWrapper(a.AttributeConstructor!, metadataLoadContext);
NamedArguments = namedArguments;
ConstructorArguments = constructorArguments;
}
public override ConstructorInfo Constructor { get; }
public override IList<CustomAttributeNamedArgument> NamedArguments { get; }
public override IList<CustomAttributeTypedArgument> ConstructorArguments { get; }
}
}