If you know a type in your main assembly, you can use:
private IEnumerable<Type> GetControllers()
{
return from t in typeof(MyType).Assembly.GetTypes()
where t.IsAbstract == false
where typeof(Controller).IsAssignableFrom(t)
where t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)
select t;
}
Replace MyType
with the known type.
I use this in a base class with this.GetType()
instead of typeof(MyType)
, so that I can reference the assembly in which the derrived type is defined.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…