Search code examples
c#.netattributespostsharp

Override attribute applied on class


I have been testing out Postsharp, specifically the OnMethodBoundaryAspect class, creating subclass (attribute) and implementing some of my own custom logging into a CSV file.

All is working perfectly, and this may not be related to Postsharp at all

I want to apply my subclass (called MethodTraceAttribute) to my main Form object, and then apply it to a method within the Form (with different parameters).

Currently it is writing out two lines in the CSV file, one from the Form attribute, and one for the method attribute.

I want the MethodTraceAttribute on the method to override the one been applied to the class.

I'm pretty sure I've seen this in MVC3, but generally in .NET Is this possible? - I thought it might have something to do with the AttributeUsage attribute but have done some reading and nope.

EDIT:

For anyone interested, I discovered that Postsharp has an attribute similar to AttributeUsage called MulticastAttributeUsage - I have set this to MulticastTargets.Method | MulticastTargets.InstanceConstructor, AllowMultiple= false and it seems to now do what I want (no longer using the AttributeUsage attribute). I have left the question un-answered as I would like to find a .NET way of doing this


Solution

  • For anyone interested, I discovered that Postsharp has an attribute similar to AttributeUsage called MulticastAttributeUsage - I have set this to

    MulticastTargets.Method | MulticastTargets.InstanceConstructor, AllowMultiple= false

    and it seems to now do what I want (no longer using the AttributeUsage attribute).