Skip to content

Commit 8d1d98e

Browse files
committed
Renamed Ensure.Arg.OfType() to Ensure.Arg.OfGenericType() and added Ensure.Arg.OfType() which does not use DynamicallyAccessedMembersAttribute.
1 parent c0a7148 commit 8d1d98e

File tree

2 files changed

+90
-14
lines changed

2 files changed

+90
-14
lines changed

src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ public static void MinLength(
220220
}
221221

222222
/// <summary>
223-
/// Ensures that the type argument is of the expected type. Supports testing for
224-
/// open generic types.
223+
/// Ensures that the type argument is of the expected type.
225224
/// </summary>
226225
/// <param name="type">The <see cref="Type"/> argument.</param>
227226
/// <param name="expectedType">The expected <see cref="Type"/>.</param>
@@ -230,12 +229,13 @@ public static void MinLength(
230229
[MethodImpl(MethodImplOptions.AggressiveInlining)]
231230
[StackTraceHidden]
232231
public static void OfType(
233-
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
234232
Type? type,
235-
Type? expectedType,
233+
Type expectedType,
236234
[CallerArgumentExpression("type")] string? paramName = null)
237235
{
238-
if (!IsAssignableTo(type, expectedType))
236+
Ensure.Arg.NotNull(expectedType);
237+
238+
if (type != null && !expectedType.IsAssignableFrom(type))
239239
{
240240
throw new ArgumentException(
241241
$"Argument '{paramName}' is of type '{type}' but expected to be of type '{expectedType}'.",
@@ -253,34 +253,77 @@ public static void OfType(
253253
[MethodImpl(MethodImplOptions.AggressiveInlining)]
254254
[StackTraceHidden]
255255
public static void OfType<TExpected>(
256-
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
257256
Type? type,
258257
[CallerArgumentExpression("type")] string? paramName = null)
259258
{
260259
OfType(type, typeof(TExpected), paramName);
261260
}
262261

262+
/// <summary>
263+
/// Ensures that the type argument is of the expected type. Supports testing for
264+
/// open generic types.
265+
/// </summary>
266+
/// <param name="type">The <see cref="Type"/> argument.</param>
267+
/// <param name="expectedType">The expected <see cref="Type"/>.</param>
268+
/// <param name="paramName">The parameter name.</param>
269+
/// <exception cref="ArgumentException">The type argument is not of the expected type.</exception>
270+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
271+
[StackTraceHidden]
272+
public static void OfGenericType(
273+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
274+
Type? type,
275+
Type expectedType,
276+
[CallerArgumentExpression("type")] string? paramName = null)
277+
{
278+
Ensure.Arg.NotNull(expectedType);
279+
280+
if (type != null && !IsAssignableTo(type, expectedType))
281+
{
282+
throw new ArgumentException(
283+
$"Argument '{paramName}' is of type '{type}' but expected to be of type '{expectedType}'.",
284+
paramName);
285+
}
286+
}
287+
288+
/// <summary>
289+
/// Ensures that the type argument is of the expected type. Supports testing for
290+
/// open generic types.
291+
/// </summary>
292+
/// <typeparam name="TExpected">The expected <see cref="Type"/>.</typeparam>
293+
/// <param name="type">The <see cref="Type"/> argument.</param>
294+
/// <param name="paramName">The parameter name.</param>
295+
/// <exception cref="ArgumentException">The type argument is not of the expected type.</exception>
296+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
297+
[StackTraceHidden]
298+
public static void OfGenericType<TExpected>(
299+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
300+
Type? type,
301+
[CallerArgumentExpression("type")] string? paramName = null)
302+
{
303+
OfGenericType(type, typeof(TExpected), paramName);
304+
}
305+
263306
[StackTraceHidden]
264307
private static bool IsAssignableTo(
265308
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
266309
Type? givenType,
267-
Type? genericType)
310+
Type expectedType)
268311
{
269-
if (givenType == null || genericType == null)
312+
if (givenType == null || expectedType == null)
270313
{
271314
return false;
272315
}
273316

274-
if (genericType.GetTypeInfo()
317+
if (expectedType.GetTypeInfo()
275318
.IsAssignableFrom(givenType.GetTypeInfo()))
276319
{
277320
return true;
278321
}
279322

280-
return givenType == genericType
281-
|| MapsToGenericTypeDefinition(givenType, genericType)
282-
|| HasInterfaceThatMapsToGenericTypeDefinition(givenType, genericType)
283-
|| IsAssignableTo(givenType.GetTypeInfo().BaseType, genericType);
323+
return givenType == expectedType
324+
|| MapsToGenericTypeDefinition(givenType, expectedType)
325+
|| HasInterfaceThatMapsToGenericTypeDefinition(givenType, expectedType)
326+
|| IsAssignableTo(givenType.GetTypeInfo().BaseType, expectedType);
284327
}
285328

286329
[StackTraceHidden]

test/AppCoreNet.Diagnostics.Tests/EnsureTests.Arg.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ public void MinLengthThrowsForShorterString()
163163
[Theory]
164164
[InlineData(typeof(IEnumerable))]
165165
[InlineData(typeof(IEnumerable<string>))]
166-
[InlineData(typeof(IEnumerable<>))]
167166
[InlineData(typeof(ExpectedArgBaseType))]
168167
[InlineData(typeof(ExpectedArgType))]
169168
public void OfTypeDoesNotThrowForRelatedTypes(Type expectedType)
@@ -181,5 +180,39 @@ public void OfTypeThrowsForUnrelatedType()
181180
exception.ParamName.Should()
182181
.Be("param");
183182
}
183+
184+
[Fact]
185+
public void OfTypeDoesNotThrowForNullType()
186+
{
187+
Ensure.Arg.OfType(null, typeof(string));
188+
}
189+
190+
[Theory]
191+
[InlineData(typeof(IEnumerable))]
192+
[InlineData(typeof(IEnumerable<string>))]
193+
[InlineData(typeof(IEnumerable<>))]
194+
[InlineData(typeof(ExpectedArgBaseType))]
195+
[InlineData(typeof(ExpectedArgType))]
196+
public void OfGenericTypeDoesNotThrowForRelatedTypes(Type expectedType)
197+
{
198+
Type type = typeof(ExpectedArgType);
199+
Ensure.Arg.OfGenericType(type, expectedType, "param");
200+
}
201+
202+
[Fact]
203+
public void OfGenericTypeDoesNotThrowForNullType()
204+
{
205+
Ensure.Arg.OfGenericType(null, typeof(string));
206+
}
207+
208+
[Fact]
209+
public void OfGenericTypeThrowsForUnrelatedType()
210+
{
211+
var exception = Assert.Throws<ArgumentException>(
212+
() => Ensure.Arg.OfGenericType(typeof(string), typeof(ExpectedArgType), "param"));
213+
214+
exception.ParamName.Should()
215+
.Be("param");
216+
}
184217
}
185218
}

0 commit comments

Comments
 (0)