Skip to content

Commit 5847f6d

Browse files
authored
Annotate System.Reflection.Context for nullability (#54938)
The impact here is almost entirely internal; the ref only gains two question marks.
1 parent b26d811 commit 5847f6d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+331
-296
lines changed

src/libraries/System.Reflection.Context/ref/System.Reflection.Context.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public abstract partial class CustomReflectionContext : System.Reflection.Reflec
1111
protected CustomReflectionContext() { }
1212
protected CustomReflectionContext(System.Reflection.ReflectionContext source) { }
1313
protected virtual System.Collections.Generic.IEnumerable<System.Reflection.PropertyInfo> AddProperties(System.Type type) { throw null; }
14-
protected System.Reflection.PropertyInfo CreateProperty(System.Type propertyType, string name, System.Func<object, object>? getter, System.Action<object, object>? setter) { throw null; }
15-
protected System.Reflection.PropertyInfo CreateProperty(System.Type propertyType, string name, System.Func<object, object>? getter, System.Action<object, object>? setter, System.Collections.Generic.IEnumerable<System.Attribute>? propertyCustomAttributes, System.Collections.Generic.IEnumerable<System.Attribute>? getterCustomAttributes, System.Collections.Generic.IEnumerable<System.Attribute>? setterCustomAttributes) { throw null; }
14+
protected System.Reflection.PropertyInfo CreateProperty(System.Type propertyType, string name, System.Func<object, object?>? getter, System.Action<object, object?>? setter) { throw null; }
15+
protected System.Reflection.PropertyInfo CreateProperty(System.Type propertyType, string name, System.Func<object, object?>? getter, System.Action<object, object?>? setter, System.Collections.Generic.IEnumerable<System.Attribute>? propertyCustomAttributes, System.Collections.Generic.IEnumerable<System.Attribute>? getterCustomAttributes, System.Collections.Generic.IEnumerable<System.Attribute>? setterCustomAttributes) { throw null; }
1616
protected virtual System.Collections.Generic.IEnumerable<object> GetCustomAttributes(System.Reflection.MemberInfo member, System.Collections.Generic.IEnumerable<object> declaredAttributes) { throw null; }
1717
protected virtual System.Collections.Generic.IEnumerable<object> GetCustomAttributes(System.Reflection.ParameterInfo parameter, System.Collections.Generic.IEnumerable<object> declaredAttributes) { throw null; }
1818
public override System.Reflection.Assembly MapAssembly(System.Reflection.Assembly assembly) { throw null; }

src/libraries/System.Reflection.Context/src/System.Reflection.Context.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->
88
<PropertyGroup>
9-
<!-- S.R.Context has a lot nullable warnings that need to be addressed: https://github.com/dotnet/runtime/issues/54596. -->
10-
<Nullable Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETCoreApp'">annotations</Nullable>
119
<GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetFramework)' == 'netstandard2.0'">SR.PlatformNotSupported_ReflectionContext</GeneratePlatformNotSupportedAssemblyMessage>
1210
</PropertyGroup>
1311

src/libraries/System.Reflection.Context/src/System/Reflection/Context/CollectionServices.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static T[] Empty<T>()
1212
return Array.Empty<T>();
1313
}
1414

15-
public static bool CompareArrays<T>(T[] left, T[] right)
15+
public static bool CompareArrays<T>(T[] left, T[] right) where T : notnull
1616
{
1717
if (left.Length != right.Length)
1818
return false;
@@ -26,7 +26,7 @@ public static bool CompareArrays<T>(T[] left, T[] right)
2626
return true;
2727
}
2828

29-
public static int GetArrayHashCode<T>(T[] array)
29+
public static int GetArrayHashCode<T>(T[] array) where T : notnull
3030
{
3131
int hashcode = 0;
3232
foreach (T t in array)

src/libraries/System.Reflection.Context/src/System/Reflection/Context/Custom/AttributeUtils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static object[] GetCustomAttributes(CustomReflectionContext context, Cust
1515
if (!inherit)
1616
return CollectionServices.IEnumerableToArray(attributes, attributeFilterType);
1717

18-
CustomType baseMember = type.BaseType as CustomType;
18+
CustomType? baseMember = type.BaseType as CustomType;
1919

2020
if (baseMember == null)
2121
return CollectionServices.IEnumerableToArray(attributes, attributeFilterType);
@@ -55,7 +55,7 @@ public static object[] GetCustomAttributes(CustomReflectionContext context, Cust
5555
if (!inherit)
5656
return CollectionServices.IEnumerableToArray(attributes, attributeFilterType);
5757

58-
CustomMethodInfo baseMember = method.GetBaseDefinition() as CustomMethodInfo;
58+
CustomMethodInfo? baseMember = method.GetBaseDefinition() as CustomMethodInfo;
5959

6060
if (baseMember == null || baseMember.Equals(method))
6161
return CollectionServices.IEnumerableToArray(attributes, attributeFilterType);

src/libraries/System.Reflection.Context/src/System/Reflection/Context/Custom/CustomType.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33

44
using System.Collections.Generic;
55
using System.Diagnostics;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Reflection.Context.Projection;
78
using System.Reflection.Context.Virtual;
89

910
namespace System.Reflection.Context.Custom
1011
{
1112
internal sealed class CustomType : ProjectingType
1213
{
13-
private IEnumerable<PropertyInfo> _newProperties;
14+
private IEnumerable<PropertyInfo>? _newProperties;
1415

1516
public CustomType(Type template, CustomReflectionContext context)
1617
: base(template, context.Projector)
@@ -37,9 +38,9 @@ public override bool IsDefined(Type attributeType, bool inherit)
3738
return AttributeUtils.IsDefined(this, attributeType, inherit);
3839
}
3940

40-
public override bool IsInstanceOfType(object o)
41+
public override bool IsInstanceOfType([NotNullWhen(true)] object? o)
4142
{
42-
Type objectType = ReflectionContext.GetTypeForObject(o);
43+
Type objectType = ReflectionContext.GetTypeForObject(o!);
4344
return IsAssignableFrom(objectType);
4445
}
4546

@@ -64,7 +65,7 @@ public override PropertyInfo[] GetProperties(BindingFlags bindingAttr)
6465
// adding new properties declared on base types
6566
if (!getDeclaredOnly)
6667
{
67-
CustomType baseType = BaseType as CustomType;
68+
CustomType? baseType = BaseType as CustomType;
6869
while (baseType != null)
6970
{
7071
IEnumerable<PropertyInfo> newProperties = baseType.NewProperties;
@@ -81,9 +82,9 @@ public override PropertyInfo[] GetProperties(BindingFlags bindingAttr)
8182
return results.ToArray();
8283
}
8384

84-
protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
85+
protected override PropertyInfo? GetPropertyImpl(string name, BindingFlags bindingAttr, Binder? binder, Type? returnType, Type[]? types, ParameterModifier[]? modifiers)
8586
{
86-
PropertyInfo property = base.GetPropertyImpl(name, bindingAttr, binder, returnType, types, modifiers);
87+
PropertyInfo? property = base.GetPropertyImpl(name, bindingAttr, binder, returnType, types, modifiers);
8788

8889
bool getIgnoreCase = (bindingAttr & BindingFlags.IgnoreCase) == BindingFlags.IgnoreCase;
8990
bool getDeclaredOnly = (bindingAttr & BindingFlags.DeclaredOnly) == BindingFlags.DeclaredOnly;
@@ -111,7 +112,7 @@ protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindin
111112

112113
StringComparison comparison = getIgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
113114

114-
CustomType type = this;
115+
CustomType? type = this;
115116
foreach (PropertyInfo newDeclaredProperty in type.NewProperties)
116117
{
117118
if (string.Equals(newDeclaredProperty.Name, name, comparison))
@@ -163,7 +164,7 @@ public override MethodInfo[] GetMethods(BindingFlags bindingAttr)
163164
// adding new methods declared on base types
164165
if (!getDeclaredOnly)
165166
{
166-
CustomType baseType = BaseType as CustomType;
167+
CustomType? baseType = BaseType as CustomType;
167168
while (baseType != null)
168169
{
169170
// We shouldn't add a base type method directly on a subtype.
@@ -181,9 +182,9 @@ public override MethodInfo[] GetMethods(BindingFlags bindingAttr)
181182
return results.ToArray();
182183
}
183184

184-
protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
185+
protected override MethodInfo? GetMethodImpl(string name, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[]? types, ParameterModifier[]? modifiers)
185186
{
186-
MethodInfo method = base.GetMethodImpl(name, bindingAttr, binder, callConvention, types, modifiers);
187+
MethodInfo? method = base.GetMethodImpl(name, bindingAttr, binder, callConvention, types, modifiers);
187188

188189
bool getIgnoreCase = (bindingAttr & BindingFlags.IgnoreCase) == BindingFlags.IgnoreCase;
189190
bool getDeclaredOnly = (bindingAttr & BindingFlags.DeclaredOnly) == BindingFlags.DeclaredOnly;
@@ -226,7 +227,7 @@ protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAtt
226227
{
227228
if (string.Equals(newDeclaredProperty.Name, targetPropertyName, comparison))
228229
{
229-
MethodInfo accessor = getPropertyGetter ? newDeclaredProperty.GetGetMethod() : newDeclaredProperty.GetSetMethod();
230+
MethodInfo? accessor = getPropertyGetter ? newDeclaredProperty.GetGetMethod() : newDeclaredProperty.GetSetMethod();
230231
if (accessor != null)
231232
matchingMethods.Add(accessor);
232233
}
@@ -235,7 +236,7 @@ protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAtt
235236
// adding new methods declared on base types
236237
if (!getDeclaredOnly)
237238
{
238-
CustomType baseType = BaseType as CustomType;
239+
CustomType? baseType = BaseType as CustomType;
239240

240241
while (baseType != null)
241242
{
@@ -247,7 +248,7 @@ protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAtt
247248
{
248249
PropertyInfo inheritedProperty = new InheritedPropertyInfo(newBaseProperty, this);
249250

250-
MethodInfo accessor = getPropertyGetter ? inheritedProperty.GetGetMethod() : inheritedProperty.GetSetMethod();
251+
MethodInfo? accessor = getPropertyGetter ? inheritedProperty.GetGetMethod() : inheritedProperty.GetSetMethod();
251252
if (accessor != null)
252253
matchingMethods.Add(accessor);
253254
}
@@ -278,7 +279,7 @@ protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAtt
278279
if (binder == null)
279280
binder = Type.DefaultBinder;
280281

281-
return (MethodInfo)binder.SelectMethod(bindingAttr, matchingMethods.ToArray(), types, modifiers);
282+
return (MethodInfo?)binder.SelectMethod(bindingAttr, matchingMethods.ToArray(), types, modifiers);
282283
}
283284
}
284285

src/libraries/System.Reflection.Context/src/System/Reflection/Context/CustomReflectionContext.Projector.cs

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Reflection.Context.Custom;
67
using System.Reflection.Context.Projection;
78

@@ -45,7 +46,8 @@ public Assembly ProjectAssemblyIfNeeded(Assembly value)
4546
return value;
4647
}
4748

48-
public override TypeInfo ProjectType(Type value)
49+
[return: NotNullIfNotNull("value")]
50+
public override TypeInfo? ProjectType(Type? value)
4951
{
5052
if (value == null)
5153
return null;
@@ -55,7 +57,8 @@ public override TypeInfo ProjectType(Type value)
5557
return new CustomType(value, ReflectionContext);
5658
}
5759

58-
public override Assembly ProjectAssembly(Assembly value)
60+
[return: NotNullIfNotNull("value")]
61+
public override Assembly? ProjectAssembly(Assembly? value)
5962
{
6063
if (value == null)
6164
return null;
@@ -65,7 +68,8 @@ public override Assembly ProjectAssembly(Assembly value)
6568
return new CustomAssembly(value, ReflectionContext);
6669
}
6770

68-
public override Module ProjectModule(Module value)
71+
[return: NotNullIfNotNull("value")]
72+
public override Module? ProjectModule(Module? value)
6973
{
7074
if (value == null)
7175
return null;
@@ -75,7 +79,8 @@ public override Module ProjectModule(Module value)
7579
return new CustomModule(value, ReflectionContext);
7680
}
7781

78-
public override FieldInfo ProjectField(FieldInfo value)
82+
[return: NotNullIfNotNull("value")]
83+
public override FieldInfo? ProjectField(FieldInfo? value)
7984
{
8085
if (value == null)
8186
return null;
@@ -85,7 +90,8 @@ public override FieldInfo ProjectField(FieldInfo value)
8590
return new CustomFieldInfo(value, ReflectionContext);
8691
}
8792

88-
public override EventInfo ProjectEvent(EventInfo value)
93+
[return: NotNullIfNotNull("value")]
94+
public override EventInfo? ProjectEvent(EventInfo? value)
8995
{
9096
if (value == null)
9197
return null;
@@ -95,7 +101,8 @@ public override EventInfo ProjectEvent(EventInfo value)
95101
return new CustomEventInfo(value, ReflectionContext);
96102
}
97103

98-
public override ConstructorInfo ProjectConstructor(ConstructorInfo value)
104+
[return: NotNullIfNotNull("value")]
105+
public override ConstructorInfo? ProjectConstructor(ConstructorInfo? value)
99106
{
100107
if (value == null)
101108
return null;
@@ -105,7 +112,8 @@ public override ConstructorInfo ProjectConstructor(ConstructorInfo value)
105112
return new CustomConstructorInfo(value, ReflectionContext);
106113
}
107114

108-
public override MethodInfo ProjectMethod(MethodInfo value)
115+
[return: NotNullIfNotNull("value")]
116+
public override MethodInfo? ProjectMethod(MethodInfo? value)
109117
{
110118
if (value == null)
111119
return null;
@@ -115,23 +123,25 @@ public override MethodInfo ProjectMethod(MethodInfo value)
115123
return new CustomMethodInfo(value, ReflectionContext);
116124
}
117125

118-
public override MethodBase ProjectMethodBase(MethodBase value)
126+
[return: NotNullIfNotNull("value")]
127+
public override MethodBase? ProjectMethodBase(MethodBase? value)
119128
{
120129
if (value == null)
121130
return null;
122131

123-
MethodInfo method = value as MethodInfo;
132+
MethodInfo? method = value as MethodInfo;
124133
if (method != null)
125134
return ProjectMethod(method);
126135

127-
ConstructorInfo constructor = value as ConstructorInfo;
136+
ConstructorInfo? constructor = value as ConstructorInfo;
128137
if (constructor != null)
129138
return ProjectConstructor(constructor);
130139

131140
throw new InvalidOperationException(SR.Format(SR.InvalidOperation_InvalidMethodType, value.GetType()));
132141
}
133142

134-
public override PropertyInfo ProjectProperty(PropertyInfo value)
143+
[return: NotNullIfNotNull("value")]
144+
public override PropertyInfo? ProjectProperty(PropertyInfo? value)
135145
{
136146
if (value == null)
137147
return null;
@@ -141,7 +151,8 @@ public override PropertyInfo ProjectProperty(PropertyInfo value)
141151
return new CustomPropertyInfo(value, ReflectionContext);
142152
}
143153

144-
public override ParameterInfo ProjectParameter(ParameterInfo value)
154+
[return: NotNullIfNotNull("value")]
155+
public override ParameterInfo? ProjectParameter(ParameterInfo? value)
145156
{
146157
if (value == null)
147158
return null;
@@ -151,7 +162,8 @@ public override ParameterInfo ProjectParameter(ParameterInfo value)
151162
return new CustomParameterInfo(value, ReflectionContext);
152163
}
153164

154-
public override MethodBody ProjectMethodBody(MethodBody value)
165+
[return: NotNullIfNotNull("value")]
166+
public override MethodBody? ProjectMethodBody(MethodBody? value)
155167
{
156168
if (value == null)
157169
return null;
@@ -161,7 +173,8 @@ public override MethodBody ProjectMethodBody(MethodBody value)
161173
return new ProjectingMethodBody(value, this);
162174
}
163175

164-
public override LocalVariableInfo ProjectLocalVariable(LocalVariableInfo value)
176+
[return: NotNullIfNotNull("value")]
177+
public override LocalVariableInfo? ProjectLocalVariable(LocalVariableInfo? value)
165178
{
166179
if (value == null)
167180
return null;
@@ -171,7 +184,8 @@ public override LocalVariableInfo ProjectLocalVariable(LocalVariableInfo value)
171184
return new ProjectingLocalVariableInfo(value, this);
172185
}
173186

174-
public override ExceptionHandlingClause ProjectExceptionHandlingClause(ExceptionHandlingClause value)
187+
[return: NotNullIfNotNull("value")]
188+
public override ExceptionHandlingClause? ProjectExceptionHandlingClause(ExceptionHandlingClause? value)
175189
{
176190
if (value == null)
177191
return null;
@@ -181,7 +195,8 @@ public override ExceptionHandlingClause ProjectExceptionHandlingClause(Exception
181195
return new ProjectingExceptionHandlingClause(value, this);
182196
}
183197

184-
public override CustomAttributeData ProjectCustomAttributeData(CustomAttributeData value)
198+
[return: NotNullIfNotNull("value")]
199+
public override CustomAttributeData? ProjectCustomAttributeData(CustomAttributeData? value)
185200
{
186201
if (value == null)
187202
return null;
@@ -191,7 +206,8 @@ public override CustomAttributeData ProjectCustomAttributeData(CustomAttributeDa
191206
return new ProjectingCustomAttributeData(value, this);
192207
}
193208

194-
public override ManifestResourceInfo ProjectManifestResource(ManifestResourceInfo value)
209+
[return: NotNullIfNotNull("value")]
210+
public override ManifestResourceInfo? ProjectManifestResource(ManifestResourceInfo? value)
195211
{
196212
if (value == null)
197213
return null;
@@ -201,12 +217,13 @@ public override ManifestResourceInfo ProjectManifestResource(ManifestResourceInf
201217
return new ProjectingManifestResourceInfo(value, this);
202218
}
203219

204-
public override MemberInfo ProjectMember(MemberInfo value)
220+
[return: NotNullIfNotNull("value")]
221+
public override MemberInfo? ProjectMember(MemberInfo? value)
205222
{
206223
if (value == null)
207224
return null;
208225

209-
MemberInfo output = null;
226+
MemberInfo? output;
210227
switch (value.MemberType)
211228
{
212229
case MemberTypes.TypeInfo:
@@ -243,7 +260,7 @@ public override MemberInfo ProjectMember(MemberInfo value)
243260

244261
public override CustomAttributeTypedArgument ProjectTypedArgument(CustomAttributeTypedArgument value)
245262
{
246-
Type argumentType = ProjectType(value.ArgumentType);
263+
Type? argumentType = ProjectType(value.ArgumentType);
247264

248265
return new CustomAttributeTypedArgument(argumentType, value.Value);
249266
}

0 commit comments

Comments
 (0)