Is this code wrong? It’s just not returning anything:
public IEnumerable<string> GetMethodsOfReturnType(Type cls, Type ret)
{
var methods = cls.GetMethods(BindingFlags.NonPublic);
var retMethods = methods.Where(m => m.ReturnType.IsSubclassOf(ret))
.Select(m => m.Name);
return retMethods;
}
It’s returning an empty enumerator.
Note: I’m calling it on a ASP.NET MVC Controller looking for ActionResults
GetMethodsOfReturnType(typeof(ProductsController), typeof(ActionResult));
Others have pointed out fixes, but I’d like to suggest an alternative to
IsSubclassOfas well as including public methods:With
IsAssignableFrom, you don’t need to have the extra “is the return type exactly the same as the desired type” test, and also it will work with interfaces.