Fix Event Bus for null exception (#7360)

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 #4718 
Dixes #4584
This commit is contained in:
Another Developer 2016-10-27 17:56:27 -04:00 committed by Sébastien Ros
parent b160dc731d
commit 68acf2694d

View File

@ -39,18 +39,23 @@ namespace Orchard.Events {
// 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;
}
}