I've long heard that you shouldn't throw a type Exception in C#. I try to always throw a type derived from Exception.
In addition, I've also read many brief snippets over the years that says you shouldn't write C# code like:
catch(Exception e)
{
...
}
I recently read the best explanation of this in Microsoft's MSDN Magazine (Feb. 2009). The article is entitled Handling Corrupted State Exceptions and would highly recommend those programming in C# to read the full article. It also provides details about how exception handling will change a bit in .NET 4.0.
A read well worth it!
http://msdn.microsoft.com/en-us/magazine/dd419661.aspx
Just some random development ramblings mostly related to the Microsoft .NET platform.
Showing posts with label Patterns. Show all posts
Showing posts with label Patterns. Show all posts
Sunday, March 8, 2009
Monday, August 18, 2008
Plug-in architecture (dynamically loading DLLs) using LINQ
For implementing a plug-in architecture using the strategy pattern, this is my preferred way of loading some interfaces (DLLs) at runtime. Make sure you look at the second code example showing how to do the same thing in LINQ.
Now using LINQ...
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.
This can then be called using:
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");
Subscribe to:
Posts (Atom)
Can't RDP? How to enable / disable virtual machine firewall for Azure VM
Oh no! I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore. I did ...
-
Don't want Office 2007 installed on your web server to access Excel 2007 content? Here is a Visual Studio C# solution that demonstrates ...
-
I finally found a good algorithm to validate a file path (had to fix two bugs in it though). I use this static method in Windows Forms when...