Skip to content

Commit

Permalink
Test for assignability of arrays to generic interfaces. Fixes mono#2304.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbevain committed Dec 2, 2011
1 parent 137efe9 commit 7019e2b
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions mcs/class/System.Core/System.Linq.Expressions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
//

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -69,8 +70,9 @@ public static bool IsGenericImplementationOf (this Type self, Type type, out Typ

public static bool IsAssignableTo (this Type self, Type type)
{
return type.IsAssignableFrom (self) ||
ArrayTypeIsAssignableTo (self, type);
return type.IsAssignableFrom (self)
|| ArrayTypeAreAssignable (self, type)
|| ArrayTypeIsAssignableToInterface (self, type);
}

public static Type GetFirstGenericArgument (this Type self)
Expand Down Expand Up @@ -114,7 +116,7 @@ public static Type [] GetParameterTypes (this MethodBase self)
return types;
}

static bool ArrayTypeIsAssignableTo (Type type, Type candidate)
static bool ArrayTypeAreAssignable (Type type, Type candidate)
{
if (!type.IsArray || !candidate.IsArray)
return false;
Expand All @@ -125,6 +127,17 @@ static bool ArrayTypeIsAssignableTo (Type type, Type candidate)
return type.GetElementType ().IsAssignableTo (candidate.GetElementType ());
}

static bool ArrayTypeIsAssignableToInterface (Type type, Type candidate)
{
if (!type.IsArray)
return false;

if (!(candidate.IsGenericInstanceOf (typeof (IList<>)) || candidate.IsGenericInstanceOf (typeof (ICollection<>)) || candidate.IsGenericInstanceOf (typeof (IEnumerable<>))))
return false;

return type.GetElementType () == candidate.GetFirstGenericArgument ();
}

public static void OnFieldOrProperty (this MemberInfo self,
Action<FieldInfo> onfield, Action<PropertyInfo> onprop)
{
Expand Down

0 comments on commit 7019e2b

Please sign in to comment.