1+ using mdoc . Mono . Documentation . Updater ;
2+ using Mono . Cecil ;
3+ using Mono . Documentation . Util ;
4+ using System ;
5+ using System . Collections . ObjectModel ;
6+
7+ namespace Mono . Documentation . Updater
8+ {
9+ public class AttributeParserContext : IAttributeParserContext
10+ {
11+ private int nullableAttributeIndex ;
12+ private int dynamicAttributeIndex ;
13+ private ICustomAttributeProvider provider ;
14+ private ReadOnlyCollection < bool ? > nullableAttributeFlags ;
15+ private ReadOnlyCollection < bool > dynamicAttributeFlags ;
16+
17+ private AttributeParserContext ( ICustomAttributeProvider provider )
18+ {
19+ this . provider = provider ;
20+
21+ ReadDynamicAttribute ( ) ;
22+ ReadNullableAttribute ( ) ;
23+ }
24+
25+ private bool ExistsNullableAttribute
26+ {
27+ get
28+ {
29+ return nullableAttributeFlags . Count > 0 ;
30+ }
31+ }
32+
33+ private bool HasSameNullableValue
34+ {
35+ get
36+ {
37+ return nullableAttributeFlags . Count == 1 ;
38+ }
39+ }
40+
41+ public static IAttributeParserContext Create ( ICustomAttributeProvider provider )
42+ {
43+ return new AttributeParserContext ( provider ) ;
44+ }
45+
46+ public void NextDynamicFlag ( )
47+ {
48+ dynamicAttributeIndex ++ ;
49+ }
50+
51+ public bool IsDynamic ( )
52+ {
53+ return dynamicAttributeFlags != null && ( dynamicAttributeFlags . Count == 0 || dynamicAttributeFlags [ dynamicAttributeIndex ] ) ;
54+ }
55+
56+ public bool IsNullable ( )
57+ {
58+ if ( ExistsNullableAttribute )
59+ {
60+ if ( HasSameNullableValue )
61+ {
62+ return nullableAttributeFlags [ 0 ] . IsTrue ( ) ;
63+ }
64+
65+ if ( nullableAttributeIndex < nullableAttributeFlags . Count )
66+ {
67+ return nullableAttributeFlags [ nullableAttributeIndex ++ ] . IsTrue ( ) ;
68+ }
69+
70+ throw new IndexOutOfRangeException ( "You are out of range in the nullable attribute values, please call the method for each nullable checking only once." ) ;
71+ }
72+
73+ return false ;
74+ }
75+
76+ private void ReadDynamicAttribute ( )
77+ {
78+ DynamicTypeProvider dynamicTypeProvider = new DynamicTypeProvider ( provider ) ;
79+ var dynamicTypeFlags = dynamicTypeProvider . GetDynamicTypeFlags ( ) ;
80+ if ( dynamicTypeFlags != null )
81+ {
82+ dynamicAttributeFlags = new ReadOnlyCollection < bool > ( dynamicTypeFlags ) ;
83+ }
84+ }
85+
86+ private void ReadNullableAttribute ( )
87+ {
88+ NullableReferenceTypeProvider nullableReferenceTypeProvider = new NullableReferenceTypeProvider ( provider ) ;
89+ nullableAttributeFlags = new ReadOnlyCollection < bool ? > ( nullableReferenceTypeProvider . GetNullableReferenceTypeFlags ( ) ) ;
90+ }
91+ }
92+ }
0 commit comments