Skip to content

Commit

Permalink
Add support for arrays types which contain generic parameters (Hangfi…
Browse files Browse the repository at this point in the history
  • Loading branch information
aidmsu authored and odinserj committed Nov 24, 2016
1 parent b1a246d commit cc61a19
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Hangfire.Core/Common/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ private static bool TypesMatchRecursive(TypeInfo parameterType, TypeInfo actualT

if (parameterType.ContainsGenericParameters)
{
if (parameterType.IsArray)
{
// Return false if parameterType is array whereas actualType isn't
if (!actualType.IsArray) return false;

var parameterElementType = parameterType.GetElementType();
var actualElementType = actualType.GetElementType();

return TypesMatchRecursive(parameterElementType.GetTypeInfo(), actualElementType.GetTypeInfo(), genericArguments);
}

if (!actualType.IsGenericType || parameterType.GetGenericTypeDefinition() != actualType.GetGenericTypeDefinition())
{
return false;
Expand Down
33 changes: 33 additions & 0 deletions tests/Hangfire.Core.Tests/Common/TypeExtensionsFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,35 @@ public void GetNonOpenMatchingMethod_ReturnsNull_WhenParameterTypeIsMatchedByGen

Assert.Equal(null, method);
}

[Fact]
public void GetNonOpenMatchingMethod_ReturnsCorrectMethod_WhenParameterTypeIsGenericArray()
{
var method = TypeExtensions.GetNonOpenMatchingMethod(typeof(NonGenericClass), "GenericMethod",
new[] { typeof(string[]) });

Assert.Equal("GenericMethod", method.Name);
Assert.Equal(typeof(string[]), method.GetParameters()[0].ParameterType);
}

[Fact]
public void GetNonOpenMatchingMethod_ReturnsCorrectMethod_WhenParameterTypeIsComplicatedGenericArray()
{
var method = TypeExtensions.GetNonOpenMatchingMethod(typeof(NonGenericClass), "GenericMethod",
new[] { typeof(List<int>[]) });

Assert.Equal("GenericMethod", method.Name);
Assert.Equal(typeof(List<int>[]), method.GetParameters()[0].ParameterType);
}

[Fact]
public void GetNonOpenMatchingMethod_ReturnsNull_WhenMatchingGenricMethodNotBeFound()
{
var method = TypeExtensions.GetNonOpenMatchingMethod(typeof(NonGenericClass), "GenericMethod",
new[] { typeof(Tuple<List<int>, string>) });

Assert.Null(method);
}
}

public class GenericClass<T1>
Expand Down Expand Up @@ -315,6 +344,10 @@ public void GenericMethod<T>(int arg0, T arg1, double arg2) { }

public void GenericMethod<T>(Tuple<T, List<int>> arg) { }

public void GenericMethod<T>(T[] arg) { }

public void GenericMethod<T>(List<T>[] arg) { }

public class NestedNonGenericClass
{
public class DoubleNestedNonGenericClass
Expand Down

0 comments on commit cc61a19

Please sign in to comment.