Skip to content

Commit

Permalink
Nullable for Type class (dotnet/coreclr#23489)
Browse files Browse the repository at this point in the history
System.Type, System.Reflection nullability

Signed-off-by: dotnet-bot <[email protected]>
  • Loading branch information
buyaa-n authored and dotnet-bot committed May 2, 2019
1 parent 3397f83 commit 866509a
Show file tree
Hide file tree
Showing 64 changed files with 511 additions and 470 deletions.
8 changes: 4 additions & 4 deletions src/System.Private.CoreLib/shared/System/AppDomain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ public void SetThreadPrincipal(IPrincipal principal)
case PrincipalPolicy.UnauthenticatedPrincipal:
if (s_getUnauthenticatedPrincipal == null)
{
Type type = Type.GetType("System.Security.Principal.GenericPrincipal, System.Security.Claims", throwOnError: true);
MethodInfo mi = type.GetMethod("GetDefaultInstance", BindingFlags.NonPublic | BindingFlags.Static);
Type type = Type.GetType("System.Security.Principal.GenericPrincipal, System.Security.Claims", throwOnError: true)!;
MethodInfo? mi = type.GetMethod("GetDefaultInstance", BindingFlags.NonPublic | BindingFlags.Static);
Debug.Assert(mi != null);
// Don't throw PNSE if null like for WindowsPrincipal as UnauthenticatedPrincipal should
// be available on all platforms.
Expand All @@ -414,8 +414,8 @@ public void SetThreadPrincipal(IPrincipal principal)
case PrincipalPolicy.WindowsPrincipal:
if (s_getWindowsPrincipal == null)
{
Type type = Type.GetType("System.Security.Principal.WindowsPrincipal, System.Security.Principal.Windows", throwOnError: true);
MethodInfo mi = type.GetMethod("GetDefaultInstance", BindingFlags.NonPublic | BindingFlags.Static);
Type type = Type.GetType("System.Security.Principal.WindowsPrincipal, System.Security.Principal.Windows", throwOnError: true)!;
MethodInfo? mi = type.GetMethod("GetDefaultInstance", BindingFlags.NonPublic | BindingFlags.Static);
if (mi == null)
{
throw new PlatformNotSupportedException(SR.PlatformNotSupported_Principal);
Expand Down
21 changes: 11 additions & 10 deletions src/System.Private.CoreLib/shared/System/Attribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using System.Diagnostics;
using System.Reflection;

Expand All @@ -15,7 +16,7 @@ public abstract partial class Attribute
protected Attribute() { }

#if !CORERT
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (obj == null)
return false;
Expand All @@ -25,7 +26,7 @@ public override bool Equals(object obj)

Type thisType = this.GetType();
object thisObj = this;
object thisResult, thatResult;
object? thisResult, thatResult;

while (thisType != typeof(Attribute))
{
Expand All @@ -41,7 +42,7 @@ public override bool Equals(object obj)
return false;
}
}
thisType = thisType.BaseType;
thisType = thisType.BaseType!;
}

return true;
Expand All @@ -54,11 +55,11 @@ public override int GetHashCode()
while (type != typeof(Attribute))
{
FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
object vThis = null;
object? vThis = null;

for (int i = 0; i < fields.Length; i++)
{
object fieldValue = fields[i].GetValue(this);
object? fieldValue = fields[i].GetValue(this);

// The hashcode of an array ignores the contents of the array, so it can produce
// different hashcodes for arrays with the same contents.
Expand All @@ -74,15 +75,15 @@ public override int GetHashCode()
if (vThis != null)
return vThis.GetHashCode();

type = type.BaseType;
type = type.BaseType!;
}

return type.GetHashCode();
}
#endif

// Compares values of custom-attribute fields.
private static bool AreFieldValuesEqual(object thisValue, object thatValue)
private static bool AreFieldValuesEqual(object? thisValue, object? thatValue)
{
if (thisValue == null && thatValue == null)
return true;
Expand All @@ -99,8 +100,8 @@ private static bool AreFieldValuesEqual(object thisValue, object thatValue)
return false;
}

Array thisValueArray = thisValue as Array;
Array thatValueArray = thatValue as Array;
Array thisValueArray = (Array)thisValue;
Array thatValueArray = (Array)thatValue;
if (thisValueArray.Length != thatValueArray.Length)
{
return false;
Expand Down Expand Up @@ -132,7 +133,7 @@ private static bool AreFieldValuesEqual(object thisValue, object thatValue)

public virtual object TypeId => GetType();

public virtual bool Match(object obj) => Equals(obj);
public virtual bool Match(object? obj) => Equals(obj);

public virtual bool IsDefaultAttribute() => false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ bool TryConvertFromInvariantString(Type? typeToConvert, string? stringValue, out
// lazy init reflection objects
if (s_convertFromInvariantString == null)
{
Type typeDescriptorType = Type.GetType("System.ComponentModel.TypeDescriptor, System.ComponentModel.TypeConverter", throwOnError: false);
Type? typeDescriptorType = Type.GetType("System.ComponentModel.TypeDescriptor, System.ComponentModel.TypeConverter", throwOnError: false);
MethodInfo? mi = typeDescriptorType?.GetMethod("ConvertFromInvariantString", BindingFlags.NonPublic | BindingFlags.Static);
Volatile.Write(ref s_convertFromInvariantString, mi == null ? new object() : mi.CreateDelegate(typeof(Func<Type, string, object>)));
}
Expand Down
Loading

0 comments on commit 866509a

Please sign in to comment.