目前我有一个类似的列表:
CADisplayLink
当我添加和删除类时,我宁愿不必维护长列表。 有没有办法在.net core 2中找到并实例化扩展另一个类的所有类?
提前感谢您的帮助。
答案 0 :(得分:5)
是的,请参阅Getting all types that implement an interface
fn call(&self, req: hyper::server::Request) -> Self::Future {
let mut response: Response = Response::new();
match (req.method(), req.path()) {
(&Method::Post, "/assets") => {
if let Some(len) = req.headers().get::<ContentLength>() {
response.headers_mut().set(len.clone());
}
response = response.with_body("req.body()");
}
_ => {
response.set_status(StatusCode::NotFound);
}
};
futures::future::ok(response)
}
如果它们都具有无参数构造函数,则可以使用Activator.CreateInstance创建实例
var type = typeof(Algo);
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p));