Skip to content

Commit a31a34b

Browse files
committed
Feat: MatchesGenericType
1 parent c63e66d commit a31a34b

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
// ReSharper disable once CheckNamespace
5+
namespace System;
6+
7+
// ---------------------------------------------------------------------------------------------------------------------
8+
// Code
9+
// ---------------------------------------------------------------------------------------------------------------------
10+
public static class TypeExtensions {
11+
public static bool MatchesGenericType(this Type type, Type genericType) {
12+
if (!type.IsGenericType) return false;
13+
return type.GetGenericTypeDefinition() == genericType;
14+
}
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
namespace Tests.CodeOfChaos.Extensions;
5+
6+
// ---------------------------------------------------------------------------------------------------------------------
7+
// Code
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
public class TypeExtensionTests {
10+
[Test]
11+
public async Task MatchesGenericType_ShouldReturnTrue() {
12+
// Arrange
13+
var type = typeof(List<>);
14+
var genericType = typeof(List<>);
15+
16+
// Act
17+
var result = type.MatchesGenericType(genericType);
18+
19+
// Assert
20+
await Assert.That(result).IsTrue();
21+
}
22+
}

0 commit comments

Comments
 (0)