Reputation: 3637
I was thinking that is it possible to do exception handling using Attribute, rather than write "try...catch" in every single method.
for example, now, each of my method look like this:
public void DoSomething()
{
try
{
// do something
}
catch (Exception ex)
{
// exception handling rules, always the same.
throw;
}
}
I want:
[HandleException]
public void DoSomething()
{
// do something
}
Is it possible?
Upvotes: 6
Views: 2872
Reputation: 371
You can do this with Fody
There is a similar plugin to handle exceptions on async code if you want to check it.
https://github.com/Fody/AsyncErrorHandler
And here is one tutorial.
https://michielsioen.be/2017-10-21-il-weaving/
https://michielsioen.be/2017-12-28-il-weaving-pt2/
Upvotes: 0
Reputation: 9240
Also, you can write your own MSBuild Task.
Here you can find an opensource project. During compile time it injects INotifyPropertyChanged
supporting code in property setters using Mono.Cecil
lib.
Upvotes: 0
Reputation: 2593
This can be done via AOP (aspect oriented programming). One of the frameworks for doing so is PostSharp. See a sample http://www.sharpcrafters.com/solutions/exception
Upvotes: 3