Skip to content

Commit ff09bae

Browse files
committed
Merge remote-tracking branch 'origin/feature/connection-strings'
2 parents 791e03e + dbc1429 commit ff09bae

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

CodeJam.Main.Tests/Reflection/ReflectionExtensionsTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Collections.ObjectModel;
55
using System.ComponentModel;
6+
using System.IO;
67
using System.Runtime.CompilerServices;
78

89
using CodeJam.Targeting;
@@ -317,6 +318,14 @@ public TestCompilerGeneratedCaseAttribute() : base(GetCompilerGeneratedClosure(0
317318
public bool IsBrowsable([NotNull] Type type) => type.IsBrowsable();
318319
#endif
319320

321+
[TestCase(typeof(int), typeof(object), ExpectedResult = true)]
322+
[TestCase(typeof(FileStream), typeof(Stream), ExpectedResult = true)]
323+
[TestCase(typeof(int[]), typeof(IList), ExpectedResult = true)]
324+
[TestCase(typeof(int), typeof(int?), ExpectedResult = true)]
325+
[TestCase(typeof(int), typeof(long), ExpectedResult = false)]
326+
[TestCase(typeof(Stream), typeof(FileStream), ExpectedResult = false)]
327+
public bool CanBeAssignedTo([NotNull] Type type, [NotNull] Type targetType) => type.IsAssignableTo(targetType);
328+
320329
#region Inner types
321330
private class A
322331
{

CodeJam.Main/Reflection/ReflectionExtensions.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,5 +662,42 @@ public static bool IsBrowsable([NotNull] this Type type) =>
662662
[Pure]
663663
public static bool IsBrowsable([NotNull] this MemberInfo member) =>
664664
member.GetCustomAttribute<BrowsableAttribute>()?.Browsable ?? true;
665+
666+
/// <summary>
667+
/// Determines whether instance of <typeparamref name="T"/> can be assigned to instance of current type.
668+
/// </summary>
669+
/// <typeparam name="T">Type to check</typeparam>
670+
/// <param name="type">Target type.</param>
671+
/// <returns>
672+
/// <c>true</c> if instance of <typeparamref name="T"/> can be assigned to instance of current type; otherwise, <c>false</c>.
673+
/// </returns>
674+
[Pure]
675+
public static bool IsAssignableFrom<T>([NotNull] this Type type) => type.IsAssignableFrom(typeof(T));
676+
677+
/// <summary>
678+
/// Determines whether instance of current type can be assigned to instance of <typeparamref name="T"/>.
679+
/// </summary>
680+
/// <typeparam name="T">Target type</typeparam>
681+
/// <param name="type">The type to check.</param>
682+
/// <returns>
683+
/// <c>true</c> if instance of current type can be assigned to instance of <typeparamref name="T"/>; otherwise, <c>false</c>.
684+
/// </returns>
685+
[Pure]
686+
public static bool IsAssignableTo<T>([NotNull] this Type type) => typeof(T).IsAssignableFrom(type);
687+
688+
/// <summary>
689+
/// Determines whether instance of current type can be assigned to instance of <paramref name="targetType"/>.
690+
/// </summary>
691+
/// <param name="type">The type to check.</param>
692+
/// <param name="targetType">Target type.</param>
693+
/// <returns>
694+
/// <c>true</c> if instance of current type can be assigned to instance of <paramref name="targetType"/>; otherwise, <c>false</c>.
695+
/// </returns>
696+
[Pure]
697+
public static bool IsAssignableTo([NotNull] this Type type, Type targetType)
698+
{
699+
Code.NotNull(targetType, nameof(targetType));
700+
return targetType.IsAssignableFrom(type);
701+
}
665702
}
666703
}

0 commit comments

Comments
 (0)