taylorjonl taylorjonl - 1 month ago 12
C# Question

PostSharp aspect build error on class derived from generic type

I am trying to use the Dispatched aspect on a class to push execution to the UI thread but I am getting an error at build time. Here is the error:


You cannot add an aspect to a generic type instance. Add the aspect to the corresponding generic type definition.


It seems to have a problem with me putting the aspect on a class that derives from a generic type.

Here is a simplified code sample that can reproduce this:

interface IView
{
}

class Presenter<T>
{
}

class DataPresenter : Presenter<IView>
{
[Dispatched]
void DoSomething()
{
}
}


Am I missing something or is it not possible to use aspects on a class that inherits from generic types?

Answer

As Daniel mentions in the comments, this is a bug in PostSharp but I found a work around. I modified the presenter class:

class Presenter<T> : DispatcherObject, IDispatcherObject
{
    IDispatcher IDispatcherObject.Dispatcher => DispatcherFactory.GetDispatcherForWindowsDispatcher(Dispatcher);
}

In my code Presenter was already inheriting from DispatcherObject, so implementing the IDispatcherObject interface fixed my issue.