Brett Rossier
Brett Rossier

Reputation: 3482

PostSharp: Exclude multicasted attribute from classes within same namespace that derive from common base

I'm multicasting an interface introduction at the assembly level over all EF entities in a given namespace, but I want to exclude from introduction any classes within that namespace that derive from DbContext. Not sure how to do this without explicitly excluding each DbContext-derived class by name. :(

[assembly: MyApi.IntroduceAspect(AttributeTargetTypes = "MyApi.Models.*")]
[assembly: MyApi.IntroduceAspect(AttributeTargetTypes = "MyApi.Models.SomeContext", AttributeExclude = true)]
[assembly: MyApi.IntroduceAspect(AttributeTargetTypes = "MyApi.Models.SomeOtherContext", AttributeExclude = true)]

Upvotes: 1

Views: 301

Answers (1)

AlexD
AlexD

Reputation: 5101

Attribute multicasting doesn't allow to filter on the base class of the target type. For this kind of filtering you can implement CompileTimeValidate method in your aspect and return false if the target type derives from DbContext.

public override bool CompileTimeValidate(Type targetType) 
{
    if (typeof(DbContext).IsAssignableFrom(targetType))
        return false;

    return true;
}

Documentation link: http://doc.postsharp.net/aspect-validation

Upvotes: 1

Related Questions