• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Sholtee / proxygen / 1046

27 Apr 2025 01:21PM UTC coverage: 92.705% (+0.6%) from 92.112%
1046

push

appveyor

Sholtee
Merge branch 'v10-preview1'

4791 of 5168 relevant lines covered (92.71%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.7
/SRC/Private/SyntaxFactories/ClassSyntaxFactoryBase.Property.cs
1
/********************************************************************************
2
* ClassSyntaxFactoryBase.Property.cs                                            *
3
*                                                                               *
4
* Author: Denes Solti                                                           *
5
********************************************************************************/
6
using System;
7
using System.Collections.Generic;
8
using System.Diagnostics;
9
using System.Linq;
10

11
using Microsoft.CodeAnalysis.CSharp;
12
using Microsoft.CodeAnalysis.CSharp.Syntax;
13

14
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
15

16
namespace Solti.Utils.Proxy.Internals
17
{
18
    internal partial class ClassSyntaxFactoryBase
19
    {
20
        /// <summary>
21
        /// <code>
22
        /// target.Property                           
23
        /// // OR                                         
24
        /// target.Property[index]
25
        /// </code>
26
        /// </summary>
27
        #if DEBUG
28
        internal
29
        #endif
30
        protected ExpressionSyntax PropertyAccess(IPropertyInfo property, ExpressionSyntax? target, ITypeInfo? castTargetTo = null) => PropertyAccess
1✔
31
        (
1✔
32
            property, 
1✔
33
            target, 
1✔
34
            castTargetTo, 
1✔
35
            property.Indices.Select(static param => Argument(IdentifierName(param.Name)))
1✔
36
        );
1✔
37

38
        /// <summary>
39
        /// <code>
40
        /// target.Property         
41
        /// // OR        
42
        /// target.Property[index]
43
        /// </code>
44
        /// </summary>
45
        #if DEBUG
46
        internal
47
        #endif
48
        protected ExpressionSyntax PropertyAccess(IPropertyInfo property, ExpressionSyntax? target, ITypeInfo? castTargetTo = null, IEnumerable<ArgumentSyntax>? indices = null) => !property.Indices.Any()
1✔
49
            ? MemberAccess
1✔
50
            (
1✔
51
                target,
1✔
52
                property,
1✔
53
                castTargetTo
1✔
54
            )
1✔
55
            : (ExpressionSyntax) ElementAccessExpression
1✔
56
            (
1✔
57
                AmendTarget(target, property, castTargetTo),
1✔
58
                BracketedArgumentList
1✔
59
                (
1✔
60
                    arguments: indices!.ToSyntaxList()
1✔
61
                )
1✔
62
            );
1✔
63

64
        private TDeclaration ResolveProperty<TDeclaration>(IPropertyInfo property, Func<IPropertyInfo, TDeclaration> fact, CSharpSyntaxNode? getBody, CSharpSyntaxNode? setBody) where TDeclaration : BasePropertyDeclarationSyntax
65
        {
1✔
66
            TDeclaration result = fact(property);
1✔
67

68
            IMethodInfo backingMethodHavingHigherVisibility = (property.GetMethod?.AccessModifiers ?? AccessModifiers.Unknown) > (property.SetMethod?.AccessModifiers ?? AccessModifiers.Unknown)
×
69
                ? property.GetMethod!
×
70
                : property.SetMethod!;
×
71

72
            if (property.DeclaringType.Flags.HasFlag(TypeInfoFlags.IsInterface))
1✔
73
            {
1✔
74
                CheckNotStaticAbstract(property);
1✔
75

76
                result = (TDeclaration) result.WithExplicitInterfaceSpecifier
1✔
77
                (
1✔
78
                    explicitInterfaceSpecifier: ExplicitInterfaceSpecifier
1✔
79
                    (
1✔
80
                        (NameSyntax) ResolveType(property.DeclaringType)
1✔
81
                    )
1✔
82
                );
1✔
83
            }
1✔
84
            else
85
            {
1✔
86
                List<SyntaxKind> tokens = [..ResolveAccessModifiers(backingMethodHavingHigherVisibility)];
1✔
87

88
                IMemberInfo underlyingMethod = property.GetMethod ?? property.SetMethod!;
×
89

90
                tokens.Add(underlyingMethod.IsVirtual || underlyingMethod.IsAbstract ? SyntaxKind.OverrideKeyword : SyntaxKind.NewKeyword);
×
91

92
                result = (TDeclaration) result.WithModifiers
×
93
                (
×
94
                    TokenList
×
95
                    (
×
96
                        tokens.Select(Token)
×
97
                    )
×
98
                );
×
99
            }
1✔
100

101
            List<AccessorDeclarationSyntax> accessors = new(2);
1✔
102

103
            if (property.GetMethod is not null) accessors.Add
1✔
104
            (
1✔
105
                ResolveAccessor(property.GetMethod, getBody, SyntaxKind.GetAccessorDeclaration)
1✔
106
            );
1✔
107

108
            if (property.SetMethod is not null) accessors.Add
1✔
109
            (
1✔
110
                ResolveAccessor(property.SetMethod, setBody, SyntaxKind.SetAccessorDeclaration)
1✔
111
            );
1✔
112

113
            return (TDeclaration) result.WithAccessorList
1✔
114
            (
1✔
115
                accessorList: AccessorList
1✔
116
                (
1✔
117
                    accessors: List(accessors)
1✔
118
                )
1✔
119
            );
1✔
120

121
            AccessorDeclarationSyntax ResolveAccessor(IMethodInfo backingMethod, CSharpSyntaxNode? body, SyntaxKind kind)
122
            {
1✔
123
                Debug.Assert(backingMethod is not null, "Backing method cannot be null");
1✔
124

125
                //
126
                // Accessor cannot have higher visibility than the property's
127
                //
128

129
                IEnumerable<SyntaxKind> modifiers = backingMethod!.AccessModifiers < backingMethodHavingHigherVisibility.AccessModifiers
1✔
130
                    ? ResolveAccessModifiers(backingMethod)
1✔
131
                    : [];
1✔
132

133
                return ClassSyntaxFactoryBase.ResolveAccessor(kind, body, modifiers);
1✔
134
            }
1✔
135
        }
1✔
136

137
        /// <summary>
138
        /// <code>
139
        /// int IInterface[T].Prop
140
        /// {                      
141
        ///   get{...}            
142
        ///   set{...}           
143
        /// }                    
144
        /// </code>
145
        /// or
146
        /// <code>
147
        /// public override int Prop
148
        /// {                      
149
        ///   get{...}            
150
        ///   protected set{...}           
151
        /// }                    
152
        /// </code>
153
        /// </summary>
154
        #if DEBUG
155
        internal
156
        #endif
157
        protected PropertyDeclarationSyntax ResolveProperty(IPropertyInfo property, CSharpSyntaxNode? getBody, CSharpSyntaxNode? setBody) => ResolveProperty
1✔
158
        (
1✔
159
            property,
1✔
160
            property => PropertyDeclaration
1✔
161
            (
1✔
162
                type: ResolveType(property.Type),
1✔
163
                identifier: Identifier(property.Name)
1✔
164
            ),
1✔
165
            getBody,
1✔
166
            setBody
1✔
167
        );
1✔
168

169
        /// <summary>
170
        /// <code>
171
        /// int IInterface.this[string index, ...]
172
        /// {                                     
173
        ///   get{...}                            
174
        ///   set{...}                           
175
        /// }            
176
        /// </code>
177
        /// or
178
        /// <code>
179
        /// public override int this[string index, ...]
180
        /// {                                     
181
        ///   get{...}                            
182
        ///   protected set{...}                           
183
        /// }            
184
        /// </code>
185
        /// </summary>
186
        #if DEBUG
187
        internal
188
        #endif
189
        protected IndexerDeclarationSyntax ResolveIndexer(IPropertyInfo property, CSharpSyntaxNode? getBody, CSharpSyntaxNode? setBody) => ResolveProperty
1✔
190
        (
1✔
191
            property,
1✔
192
            property => 
1✔
193
                IndexerDeclaration
1✔
194
                (
1✔
195
                    type: ResolveType(property.Type)
1✔
196
                )
1✔
197
                .WithParameterList
1✔
198
                (
1✔
199
                    parameterList: BracketedParameterList
1✔
200
                    (
1✔
201
                        parameters: property.Indices.ToSyntaxList
1✔
202
                        (
1✔
203
                            index => Parameter
1✔
204
                            (
1✔
205
                                identifier: Identifier(index.Name)
1✔
206
                            )
1✔
207
                            .WithType
1✔
208
                            (
1✔
209
                                type: ResolveType(index.Type)
1✔
210
                            )
1✔
211
                        )
1✔
212
                    )
1✔
213
                ),
1✔
214
            getBody,
1✔
215
            setBody
1✔
216
        );
1✔
217

218
        #if DEBUG
219
        internal
220
        #endif
221
        protected virtual ClassDeclarationSyntax ResolveProperties(ClassDeclarationSyntax cls, object context) => cls;
1✔
222

223
        #if DEBUG
224
        internal
225
        #endif
226
        protected virtual ClassDeclarationSyntax ResolveProperty(ClassDeclarationSyntax cls, object context, IPropertyInfo property) => throw new NotImplementedException();
×
227
    }
228
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc