File tree Expand file tree Collapse file tree 3 files changed +46
-0
lines changed
tests/BenchmarkDotNet.Tests/Validators Expand file tree Collapse file tree 3 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,20 @@ internal static bool HasAttribute<T>(this MemberInfo? memberInfo) where T : Attr
19
19
20
20
internal static bool IsNullable ( this Type type ) => Nullable . GetUnderlyingType ( type ) != null ;
21
21
22
+ public static bool IsInitOnly ( this PropertyInfo propertyInfo )
23
+ {
24
+ var setMethodReturnParameter = propertyInfo . SetMethod ? . ReturnParameter ;
25
+ if ( setMethodReturnParameter == null )
26
+ return false ;
27
+
28
+ var isExternalInitType = typeof ( System . Runtime . CompilerServices . Unsafe ) . Assembly
29
+ . GetType ( "System.Runtime.CompilerServices.IsExternalInit" ) ;
30
+ if ( isExternalInitType == null )
31
+ return false ;
32
+
33
+ return setMethodReturnParameter . GetRequiredCustomModifiers ( ) . Contains ( isExternalInitType ) ;
34
+ }
35
+
22
36
/// <summary>
23
37
/// returns type name which can be used in generated C# code
24
38
/// </summary>
Original file line number Diff line number Diff line change @@ -46,6 +46,12 @@ private IEnumerable<ValidationError> Validate(Type type)
46
46
yield return new ValidationError ( TreatsWarningsAsErrors ,
47
47
$ "Unable to use { name } with { attributeString } because it's a { modifier } field. Please, remove the { modifier } modifier.") ;
48
48
}
49
+
50
+ if ( memberInfo is PropertyInfo propertyInfo && propertyInfo . IsInitOnly ( ) )
51
+ {
52
+ yield return new ValidationError ( TreatsWarningsAsErrors ,
53
+ $ "Unable to use { name } with { attributeString } because it's init-only. Please, provide a public setter.") ;
54
+ }
49
55
}
50
56
}
51
57
}
Original file line number Diff line number Diff line change @@ -176,5 +176,31 @@ public class PropMultiple4 : Base
176
176
[ ParamsSource ( nameof ( Source ) ) ]
177
177
public bool Input { get ; set ; }
178
178
}
179
+
180
+ #if NET5_0_OR_GREATER
181
+
182
+ [ Fact ] public void InitOnly1Test ( ) => Check < InitOnly1 > ( nameof ( InitOnly1 . Input ) , "init-only" , P ) ;
183
+ [ Fact ] public void InitOnly2Test ( ) => Check < InitOnly2 > ( nameof ( InitOnly2 . Input ) , "init-only" , Pa ) ;
184
+ [ Fact ] public void InitOnly3Test ( ) => Check < InitOnly3 > ( nameof ( InitOnly3 . Input ) , "init-only" , Ps ) ;
185
+
186
+ public class InitOnly1 : Base
187
+ {
188
+ [ Params ( false , true ) ]
189
+ public bool Input { get ; init ; }
190
+ }
191
+
192
+ public class InitOnly2 : Base
193
+ {
194
+ [ ParamsAllValues ]
195
+ public bool Input { get ; init ; }
196
+ }
197
+
198
+ public class InitOnly3 : Base
199
+ {
200
+ [ ParamsSource ( nameof ( Source ) ) ]
201
+ public bool Input { get ; init ; }
202
+ }
203
+
204
+ #endif
179
205
}
180
206
}
You can’t perform that action at this time.
0 commit comments