As I understand it, all compile-time .NET aspect-oriented-programming frameworks (such as PostSharp or Fody) can only process code that has already successfully compiled.
This presents an obstacle if you want to use an aspect from these frameworks to implement members of an abstract interface, because the class being modified would not implement the interface until after the post-build step. The post-build step never gets to run if the compilation stage fails because of the missing interface.
For example, writing an WPF MVVM application, I have an abstract interface for my view model. The interface dictates that the view model must implement a command, e.g.:
using System.Windows.Input
public interface ITestVM
{
ICommand SomeCommand { get; }
}
I would like to create a concrete implementation of this view model abstract interface, using Commander.Fody to implement the ICommand instance. Here's what I tried:
using Commander;
public class TestVM : ITestVM
{
[OnCommandCanExecute("SomeCommand")]
private bool SomeCommandCanExecute()
{
return true;
}
[OnCommand("SomeCommand")]
private void SomeCommandExecute()
{
}
}
This produces the error message "'TestVM' does not implement interface member 'ITestVM.SomeCommand'"
I believe that Commander.Fody would create an ICommand instance named SomeCommand, but it never gets a chance to do so.
Is there any way to use compile-time aspect-oriented frameworks to implement abstract interfaces? I've read the book "AOP.NET", and no such limitation is discussed in the section on compile-time weaving (pp 183-190).
This is a current known limitation of tools like PostSharp and Fody as they are post-compiler weavers, not compile-time weavers.
The best way to do it is to implement an attribute that you can use as a marker to signal a class should be modified by a weaver. Then in your weaver you would have to add the interface, and the implementation to the type marked.
PropertyChanged does a similar thing with the ImplementPropertyChangedAttribute