Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/System.Private.CoreLib/shared/System/DefaultBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public sealed override MethodBase BindToMethod(
paramArrayType = null;

// If we have named parameters then we may have a hole in the candidates array.
if (candidates[i] == null)
if (candidates[i] is null)
continue;

// Validate the parameters.
Expand Down Expand Up @@ -838,10 +838,15 @@ public static PropertyInfo ExactPropertyBinding(PropertyInfo[] match, Type retur
if (returnType != null && returnType != match[i].PropertyType)
continue;

if (bestMatch != null)
if (bestMatch is null)
{
bestMatch = match[i];
}
else
{
throw new AmbiguousMatchException(SR.Arg_AmbiguousMatchException);
}

bestMatch = match[i];
}
return bestMatch;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public EventSourceSettings Settings
/// </summary>
public static Guid GetGuid(Type eventSourceType)
{
if (eventSourceType == null)
if (eventSourceType is null)
throw new ArgumentNullException(nameof(eventSourceType));

EventSourceAttribute attrib = (EventSourceAttribute)GetCustomAttributeHelper(eventSourceType, typeof(EventSourceAttribute));
Expand Down Expand Up @@ -384,7 +384,7 @@ public static string GenerateManifest(Type eventSourceType, string assemblyPathT
/// <returns>The XML data string or null</returns>
public static string GenerateManifest(Type eventSourceType, string assemblyPathToIncludeInManifest, EventManifestOptions flags)
{
if (eventSourceType == null)
if (eventSourceType is null)
throw new ArgumentNullException(nameof(eventSourceType));

byte[] manifestBytes = EventSource.CreateManifestAndDescriptors(eventSourceType, assemblyPathToIncludeInManifest, null, flags);
Expand Down Expand Up @@ -1461,7 +1461,7 @@ private unsafe void Initialize(Guid eventSourceGuid, string eventSourceName, str

private static string GetName(Type eventSourceType, EventManifestOptions flags)
{
if (eventSourceType == null)
if (eventSourceType is null)
throw new ArgumentNullException(nameof(eventSourceType));

EventSourceAttribute attrib = (EventSourceAttribute)GetCustomAttributeHelper(eventSourceType, typeof(EventSourceAttribute), flags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public TypeAnalysis(
}

MethodInfo getterInfo = Statics.GetGetMethod(propertyInfo);
if (getterInfo == null)
if (getterInfo is null)
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public virtual IEnumerable<TypeInfo> DefinedTypes
for (int i = 0; i < types.Length; i++)
{
TypeInfo typeinfo = types[i].GetTypeInfo();
if (typeinfo == null)
if (typeinfo is null)
throw new NotSupportedException(SR.Format(SR.NotSupported_NoTypeInfo, types[i].FullName));

typeinfos[i] = typeinfo;
Expand Down Expand Up @@ -100,7 +100,7 @@ public virtual Type[] GetTypes()
public virtual object CreateInstance(string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes)
{
Type t = GetType(typeName, throwOnError: false, ignoreCase: ignoreCase);
if (t == null)
if (t is null)
return null;

return Activator.CreateInstance(t, bindingAttr, binder, args, culture, activationAttributes);
Expand Down Expand Up @@ -167,11 +167,11 @@ public override string ToString()

public static Assembly GetAssembly(Type type)
{
if (type == null)
if (type is null)
throw new ArgumentNullException(nameof(type));

Module m = type.Module;
if (m == null)
if (m is null)
return null;
else
return m.Assembly;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ protected ConstructorInfo() { }

public static bool operator ==(ConstructorInfo left, ConstructorInfo right)
{
if (object.ReferenceEquals(left, right))
if (ReferenceEquals(left, right))
{
return true;
}

if ((object)left == null || (object)right == null)
return false;

return left.Equals(right);
return (left is null || right is null) ? false : left.Equals(right);
}

public static bool operator !=(ConstructorInfo left, ConstructorInfo right) => !(left == right);
Expand Down
12 changes: 8 additions & 4 deletions src/System.Private.CoreLib/shared/System/Reflection/EventInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public virtual void AddEventHandler(object target, Delegate handler)
{
MethodInfo addMethod = GetAddMethod(nonPublic: false);

if (addMethod == null)
if (addMethod is null)
throw new InvalidOperationException(SR.InvalidOperation_NoPublicAddMethod);

#if FEATURE_COMINTEROP
Expand All @@ -84,7 +84,7 @@ public virtual void RemoveEventHandler(object target, Delegate handler)
{
MethodInfo removeMethod = GetRemoveMethod(nonPublic: false);

if (removeMethod == null)
if (removeMethod is null)
throw new InvalidOperationException(SR.InvalidOperation_NoPublicRemoveMethod);

#if FEATURE_COMINTEROP
Expand All @@ -101,11 +101,15 @@ public virtual void RemoveEventHandler(object target, Delegate handler)

public static bool operator ==(EventInfo left, EventInfo right)
{
if (object.ReferenceEquals(left, right))
if (ReferenceEquals(left, right))
{
return true;
}

if ((object)left == null || (object)right == null)
if (left is null || right is null)
{
return false;
}

return left.Equals(right);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ protected FieldInfo() { }

public static bool operator ==(FieldInfo left, FieldInfo right)
{
if (object.ReferenceEquals(left, right))
if (ReferenceEquals(left, right))
{
return true;
}

if ((object)left == null || (object)right == null)
return false;

return left.Equals(right);
return (left is null || right is null) ? false : left.Equals(right);
}

public static bool operator !=(FieldInfo left, FieldInfo right) => !(left == right);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class IntrospectionExtensions
{
public static TypeInfo GetTypeInfo(this Type type)
{
if (type == null)
if (type is null)
throw new ArgumentNullException(nameof(type));

if (type is IReflectableType reflectableType)
Expand Down
61 changes: 41 additions & 20 deletions src/System.Private.CoreLib/shared/System/Reflection/MemberInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ public virtual Module Module
// This check is necessary because for some reason, Type adds a new "Module" property that hides the inherited one instead
// of overriding.

Type type = this as Type;
if (type != null)
if (this is Type type)
{
return type.Module;
}

throw NotImplemented.ByDesign;
}
Expand All @@ -46,28 +47,48 @@ public virtual Module Module

public static bool operator ==(MemberInfo left, MemberInfo right)
{
if (object.ReferenceEquals(left, right))
if (ReferenceEquals(left, right))
{
return true;
}

if ((object)left == null || (object)right == null)
if (left is null || right is null)
{
return false;
}

if (left is Type type1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this rather call left.Equals(right) instead of this manual dispatch?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like Type is the only particularly special one where operator == is an external call and its .Equals test is a ReferenceEquals; so maybe have to manually dispatch for Type?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

operator== is external call on Type just so the JIT can recognize it as intrinsic. It is from the days when the method had to be FCall in order to be recognized as intrinsic.

It should be fine to just call Equals for Type as well.

{
// Type has special handling via operator==
return (right is Type type2) ? type1 == type2 : false;
}

if (left is MethodBase method1)
{
// MethodBase has special handling via operator==
return (right is MethodBase method2) ? method1 == method2 : false;
}

Type type1, type2;
MethodBase method1, method2;
FieldInfo field1, field2;
EventInfo event1, event2;
PropertyInfo property1, property2;

if ((type1 = left as Type) != null && (type2 = right as Type) != null)
return type1 == type2;
else if ((method1 = left as MethodBase) != null && (method2 = right as MethodBase) != null)
return method1 == method2;
else if ((field1 = left as FieldInfo) != null && (field2 = right as FieldInfo) != null)
return field1 == field2;
else if ((event1 = left as EventInfo) != null && (event2 = right as EventInfo) != null)
return event1 == event2;
else if ((property1 = left as PropertyInfo) != null && (property2 = right as PropertyInfo) != null)
return property1 == property2;
if (left is FieldInfo field1)
{
// FieldInfo operator== calls Equals after same reference and null checks above,
// so just call Equals directly.
return (right is FieldInfo field2) ? field1.Equals(field2) : false;
}

if (left is EventInfo event1)
{
// EventInfo operator== calls Equals after same reference and null checks above,
// so just call Equals directly.
return (right is EventInfo event2) ? event1.Equals(event2) : false;
}

if (left is PropertyInfo property1)
{
// PropertyInfo operator== calls Equals after same reference and null checks above,
// so just call Equals directly.
return (right is PropertyInfo property2) ? property1.Equals(property2) : false;
}

return false;
}
Expand Down
26 changes: 18 additions & 8 deletions src/System.Private.CoreLib/shared/System/Reflection/MethodBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,29 @@ public bool IsConstructor

public static bool operator ==(MethodBase left, MethodBase right)
{
if (object.ReferenceEquals(left, right))
if (ReferenceEquals(left, right))
{
return true;
}

if ((object)left == null || (object)right == null)
if (left is null || right is null)
{
return false;
}

MethodInfo method1, method2;
ConstructorInfo constructor1, constructor2;
if (left is MethodInfo method1)
{
// MethodInfo operator== calls Equals after same reference and null checks above,
// so just call Equals directly.
return (right is MethodInfo method2) ? method1.Equals(method2) : false;
}

if ((method1 = left as MethodInfo) != null && (method2 = right as MethodInfo) != null)
return method1 == method2;
else if ((constructor1 = left as ConstructorInfo) != null && (constructor2 = right as ConstructorInfo) != null)
return constructor1 == constructor2;
if (left is ConstructorInfo constructor1)
{
// ConstructorInfo operator== calls Equals after same reference and null checks above,
// so just call Equals directly.
return (right is ConstructorInfo constructor2) ? constructor1.Equals(constructor2) : false;
}

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ protected MethodInfo() { }

public static bool operator ==(MethodInfo left, MethodInfo right)
{
if (object.ReferenceEquals(left, right))
if (ReferenceEquals(left, right))
{
return true;
}

if ((object)left == null || (object)right == null)
return false;

return left.Equals(right);
return (left is null || right is null) ? false : left.Equals(right);
}

public static bool operator !=(MethodInfo left, MethodInfo right) => !(left == right);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder
throw new ArgumentNullException(nameof(types));
for (int i = 0; i < types.Length; i++)
{
if (types[i] == null)
if (types[i] is null)
throw new ArgumentNullException(nameof(types));
}
return GetMethodImpl(name, bindingAttr, binder, callConvention, types, modifiers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected ParameterInfo() { }

public virtual bool IsDefined(Type attributeType, bool inherit)
{
if (attributeType == null)
if (attributeType is null)
throw new ArgumentNullException(nameof(attributeType));

return false;
Expand All @@ -41,7 +41,7 @@ public virtual bool IsDefined(Type attributeType, bool inherit)
public virtual object[] GetCustomAttributes(bool inherit) => Array.Empty<object>();
public virtual object[] GetCustomAttributes(Type attributeType, bool inherit)
{
if (attributeType == null)
if (attributeType is null)
throw new ArgumentNullException(nameof(attributeType));

return Array.Empty<object>();
Expand All @@ -57,7 +57,7 @@ public object GetRealObject(StreamingContext context)
// Once all the serializable fields have come in we can set up the real
// instance based on just two of them (MemberImpl and PositionImpl).

if (MemberImpl == null)
if (MemberImpl is null)
throw new SerializationException(SR.Serialization_InsufficientState);

ParameterInfo[] args = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private Pointer(void* ptr, Type ptrType)

public static object Box(void* ptr, Type type)
{
if (type == null)
if (type is null)
throw new ArgumentNullException(nameof(type));
if (!type.IsPointer)
throw new ArgumentException(SR.Arg_MustBePointer, nameof(ptr));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,15 @@ protected PropertyInfo() { }

public static bool operator ==(PropertyInfo left, PropertyInfo right)
{
if (object.ReferenceEquals(left, right))
if (ReferenceEquals(left, right))
{
return true;
}

if ((object)left == null || (object)right == null)
if (left is null || right is null)
{
return false;
}

return left.Equals(right);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal sealed class SignatureConstructedGenericType : SignatureType
// intended user of this constructor.
internal SignatureConstructedGenericType(Type genericTypeDefinition, Type[] typeArguments)
{
if (genericTypeDefinition == null)
if (genericTypeDefinition is null)
throw new ArgumentNullException(nameof(genericTypeDefinition));

if (typeArguments == null)
Expand All @@ -23,7 +23,7 @@ internal SignatureConstructedGenericType(Type genericTypeDefinition, Type[] type
typeArguments = (Type[])(typeArguments.Clone());
for (int i = 0; i < typeArguments.Length; i++)
{
if (typeArguments[i] == null)
if (typeArguments[i] is null)
throw new ArgumentNullException(nameof(typeArguments));
}

Expand Down
Loading