Skip to content

Commit e6bd2b7

Browse files
committed
Prefer is for type checking, where possible
Prefer `is` over either `Equals(typeof())` or `IsAssignableFrom()`, assuming a constant type and a positive check.
1 parent bf469db commit e6bd2b7

4 files changed

Lines changed: 10 additions & 12 deletions

File tree

OnTopic/Attributes/AttributeValueConverter.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,24 +128,22 @@ internal static class AttributeValueConverter {
128128
/*------------------------------------------------------------------------------------------------------------------------
129129
| Handle type-specific rules
130130
\-----------------------------------------------------------------------------------------------------------------------*/
131-
var type = value.GetType();
132-
133-
if (type.Equals(typeof(string))) {
131+
if (value is string) {
134132
return (string)value;
135133
}
136-
else if (type.Equals(typeof(bool)) || type.Equals(typeof(bool?))) {
134+
else if (value is bool) {
137135
return ((bool)value) ? "1" : "0";
138136
}
139-
else if (type.Equals(typeof(int)) || type.Equals(typeof(int?))) {
137+
else if (value is int) {
140138
return ((int)value).ToString(CultureInfo.InvariantCulture);
141139
}
142-
else if (type.Equals(typeof(double)) || type.Equals(typeof(double?))) {
140+
else if (value is double) {
143141
return ((double)value).ToString(CultureInfo.InvariantCulture);
144142
}
145-
else if (type.Equals(typeof(DateTime)) || type.Equals(typeof(DateTime?))) {
143+
else if (value is DateTime) {
146144
return ((DateTime)value).ToString(CultureInfo.InvariantCulture);
147145
}
148-
else if (type.Equals(typeof(Uri))) {
146+
else if (value is Uri) {
149147
return ((Uri)value).ToString();
150148
}
151149

OnTopic/Mapping/TopicMappingService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ private async Task<object> MapAsync(
294294
/*------------------------------------------------------------------------------------------------------------------------
295295
| Handle topics
296296
\-----------------------------------------------------------------------------------------------------------------------*/
297-
if (typeof(Topic).IsAssignableFrom(target.GetType())) {
297+
if (target is Topic) {
298298
return topic;
299299
}
300300

@@ -856,7 +856,7 @@ private IList<Topic> GetSourceCollection(Topic source, AssociationTypes associat
856856
if (
857857
sourceProperty?.GetValue(source) is IList sourcePropertyValue &&
858858
sourcePropertyValue.Count > 0 &&
859-
typeof(Topic).IsAssignableFrom(sourcePropertyValue[0]?.GetType())
859+
sourcePropertyValue[0] is Topic
860860
) {
861861
listSource = getCollection(
862862
CollectionType.MappedCollection,

OnTopic/Metadata/ContentTypeDescriptorCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void Refresh(ContentTypeDescriptor? rootContentType) {
7979
| Add all ContentTypeDescriptors to collection
8080
\-----------------------------------------------------------------------------------------------------------------------*/
8181
var contentTypeDescriptors = rootContentType
82-
.FindAll(t => typeof(ContentTypeDescriptor).IsAssignableFrom(t.GetType()))
82+
.FindAll(t => t is ContentTypeDescriptor)
8383
.Cast<ContentTypeDescriptor>();
8484
foreach (var contentType in contentTypeDescriptors) {
8585
Add((ContentTypeDescriptor)contentType);

OnTopic/Querying/TopicExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public static ReadOnlyTopicCollection FindAllByAttribute(this Topic topic, strin
288288
\-----------------------------------------------------------------------------------------------------------------------*/
289289
var contentTypeDescriptor = rootContentType?.FindFirst(t =>
290290
t.Key.Equals(topic.ContentType, StringComparison.OrdinalIgnoreCase) &&
291-
t.GetType().IsAssignableFrom(typeof(ContentTypeDescriptor))
291+
t is ContentTypeDescriptor
292292
) as ContentTypeDescriptor;
293293

294294
/*------------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)