public List<T> LoadDLL<T>(string path, string pattern)
{
List<T> plugins = new List<T>();
foreach (string s in Directory.GetFiles(Path.GetFullPath(path), pattern))
{
foreach (Type t in Assembly.LoadFile(s).GetTypes())
{
if (!t.IsAbstract && typeof(T).IsAssignableFrom(t))
{
plugins.Add((T)Activator.CreateInstance(t));
}
}
}
return plugins;
}
Now using LINQ...
public List<T> LoadDLL<T>(string path, string pattern)
{
return Directory.GetFiles(Path.GetFullPath(path), pattern)
.SelectMany(f => Assembly.LoadFile(f).GetTypes()
.Where(t => !t.IsAbstract && typeof(T).IsAssignableFrom(t))
.Select(t => (T)Activator.CreateInstance(t)))
.ToList();
}
If you want to load an assembly and all the dependent DLLs, you can use the same LINQ query, but use LoadFrom rather than LoadFile.
public List<T> LoadDLL<T>(string path, string pattern)
{
return Directory.GetFiles(Path.GetFullPath(path), pattern)
.SelectMany(f => Assembly.LoadFrom(f).GetTypes()
.Where(t => !t.IsAbstract && typeof(T).IsAssignableFrom(t))
.Select(t => (T)Activator.CreateInstance(t)))
.ToList();
}
This can then be called using:
List<Foo> foos = LoadDLL<Foo>(@".\", "*.dll");
No comments:
Post a Comment