Skip to content

Commit

Permalink
Fix Event Bus for null exception (OrchardCMS#7360)
Browse files Browse the repository at this point in the history
EventsInterceptor fails when it tries to locate a generic method on an implementation with a return type. A null check has been added to avoid the exception.

Fixes OrchardCMS#4718 
Dixes OrchardCMS#4584
  • Loading branch information
kumards authored and sebastienros committed Oct 27, 2016
1 parent b160dc7 commit 68acf26
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/Orchard/Events/EventsInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,23 @@ public static object Adjust(IEnumerable results, Type returnType) {
// static IEnumerable<T> IEnumerable.OfType<T>(this IEnumerable source)
// where T is from returnType's IEnumerable<T>
var enumerableOfTypeT = _enumerableOfTypeTDictionary.GetOrAdd( returnType, type => typeof(Enumerable).GetGenericMethod("OfType", type.GetGenericArguments(), new[] { typeof(IEnumerable) }, typeof(IEnumerable<>)));
return enumerableOfTypeT.Invoke(null, new[] { results });
return (enumerableOfTypeT != null) ? enumerableOfTypeT.Invoke(null, new[] { results }) : null;

}
}

public static class Extensions {
public static MethodInfo GetGenericMethod(this Type t, string name, Type[] genericArgTypes, Type[] argTypes, Type returnType) {
return (from m in t.GetMethods(BindingFlags.Public | BindingFlags.Static)
where m.Name == name &&
m.GetGenericArguments().Length == genericArgTypes.Length &&
m.GetParameters().Select(pi => pi.ParameterType).SequenceEqual(argTypes) &&
(m.ReturnType.IsGenericType && !m.ReturnType.IsGenericTypeDefinition ? returnType.GetGenericTypeDefinition() : m.ReturnType) == returnType
select m).Single().MakeGenericMethod(genericArgTypes);
var method = (from m in t.GetMethods(BindingFlags.Public | BindingFlags.Static)
where m.Name == name &&
m.GetGenericArguments().Length == genericArgTypes.Length &&
m.GetParameters().Select(pi => pi.ParameterType).SequenceEqual(argTypes) &&
(m.ReturnType.IsGenericType && !m.ReturnType.IsGenericTypeDefinition ? returnType.GetGenericTypeDefinition() : m.ReturnType) == returnType
select m).SingleOrDefault();
if (method != null) {
return method.MakeGenericMethod(genericArgTypes);
}
return null;

}
}
Expand Down

0 comments on commit 68acf26

Please sign in to comment.