Inheritance of Aspects
11 posts • Page 1 of 2 • 1, 2
Inheritance of Aspects
Having tried many clumsy ways to achieve what is so painless with PostSharp. I have a question, which probably reveals my lack of knowledge of AOP.
Is there anyway for an aspect attribute to be inherited by SubClasses?
For instance, suppose I have my own Aspect derived from OnFieldAccessAspect and apply it to a class, I would like all subclasses to inherit that Aspect.
Actually, I guess this is hard to achieve in general as the subclasses could be compiled at different times ....
So, if that's the reason it doesn't work, is there a way to specify that the attribute applies to subclasses as well? Something like
[assembly: MyFieldAccess(AttributeTargetAssemblies = "ConsoleApplication2", AttributeTargetTypesAndSubTypes = "ConsoleApplication2.Foo")]
--
Dan
Is there anyway for an aspect attribute to be inherited by SubClasses?
For instance, suppose I have my own Aspect derived from OnFieldAccessAspect and apply it to a class, I would like all subclasses to inherit that Aspect.
Actually, I guess this is hard to achieve in general as the subclasses could be compiled at different times ....
So, if that's the reason it doesn't work, is there a way to specify that the attribute applies to subclasses as well? Something like
[assembly: MyFieldAccess(AttributeTargetAssemblies = "ConsoleApplication2", AttributeTargetTypesAndSubTypes = "ConsoleApplication2.Foo")]
--
Dan
- ddignam
- Posts: 16
- Joined: Mon Apr 07, 2008 5:07 pm
- Full Name: Daniel Dignam
- Company: Solidworks
Re: Inheritance of Aspects
Hi Dan,
What you want is not possible out of the box.
However, you can achieve the same thing by implementing an assembly-level CompoundAspect that will inspect all base types of types of the current assembly, and add the aspects to the derived types. It is fairly easy (100-200 loc).
Gael
What you want is not possible out of the box.
However, you can achieve the same thing by implementing an assembly-level CompoundAspect that will inspect all base types of types of the current assembly, and add the aspects to the derived types. It is fairly easy (100-200 loc).
Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
got good support? consider donating to the project.
- gfraiteur
- Site Admin
- Posts: 674
- Joined: Tue Dec 18, 2007 3:09 pm
- Full Name: Gael Fraiteur
- Company: postsharp.org
Re: Inheritance of Aspects
Thanks for the pointer Gael, I'll look into it.
I'm having a great time using this tool, it's extremely powerful,
Thanks !
--
Dan
I'm having a great time using this tool, it's extremely powerful,
Thanks !
--
Dan
- ddignam
- Posts: 16
- Joined: Mon Apr 07, 2008 5:07 pm
- Full Name: Daniel Dignam
- Company: Solidworks
Re: Inheritance of Aspects
gfraiteur wrote:Hi Dan,
What you want is not possible out of the box.
However, you can achieve the same thing by implementing an assembly-level CompoundAspect that will inspect all base types of types of the current assembly, and add the aspects to the derived types. It is fairly easy (100-200 loc).
It was a lot less code for my test example, I now have a reuseable assembly with a base class and subtypes in other assemblies also inherit aspects that the base class wants them to.
--
Dan
- ddignam
- Posts: 16
- Joined: Mon Apr 07, 2008 5:07 pm
- Full Name: Daniel Dignam
- Company: Solidworks
Re: Inheritance of Aspects
Congrats!
I did not want to overpromise
.
Gael
I did not want to overpromise
Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
got good support? consider donating to the project.
- gfraiteur
- Site Admin
- Posts: 674
- Joined: Tue Dec 18, 2007 3:09 pm
- Full Name: Gael Fraiteur
- Company: postsharp.org
Re: Inheritance of Aspects
Hello,
I have managed to do this as well (fairly simply), but I cannot seem to get the aspect to be applied automatically to other assemblies which reference the assembly continaing the base class.
In the Base class assembly, I am attempting to apply the attribute like so:
and the aspect is marked up with
To get this to apply to other assembliues which reference the base class assembly, I need to also apply the aspect in that assembly. Is there something I am missing?
I am not experienced with PostSharp at all - I have just been evaluating it for a couple of days, and so far it seems great.
I have managed to do this as well (fairly simply), but I cannot seem to get the aspect to be applied automatically to other assemblies which reference the assembly continaing the base class.
In the Base class assembly, I am attempting to apply the attribute like so:
- Code: Select all
[assembly:InheritMyAspect(AttributeTargetAssemblies="*")]
and the aspect is marked up with
- Code: Select all
[MulticastAttributeUsage(MulticastTargets.Assembly,
AllowExternalAssemblies=true)]
To get this to apply to other assembliues which reference the base class assembly, I need to also apply the aspect in that assembly. Is there something I am missing?
I am not experienced with PostSharp at all - I have just been evaluating it for a couple of days, and so far it seems great.
- JeffG
- Posts: 3
- Joined: Wed Apr 16, 2008 2:58 pm
- Full Name: Jeff Goddard
- Company: RBI
Re: Inheritance of Aspects
I think it's just because the AttributeTargetAssemblies property does not support wildcards. You need to be explicit.
Gael
Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
got good support? consider donating to the project.
- gfraiteur
- Site Admin
- Posts: 674
- Joined: Tue Dec 18, 2007 3:09 pm
- Full Name: Gael Fraiteur
- Company: postsharp.org
Re: Inheritance of Aspects
Thanks for the quick answer!
As I don't know the names of the assemblies which will use the base class, I will just have to specify that they will need to include the attribute. That's not a real problemm, but I must admit it would be nicer not to have to.
As I don't know the names of the assemblies which will use the base class, I will just have to specify that they will need to include the attribute. That's not a real problemm, but I must admit it would be nicer not to have to.
- JeffG
- Posts: 3
- Joined: Wed Apr 16, 2008 2:58 pm
- Full Name: Jeff Goddard
- Company: RBI
Re: Inheritance of Aspects
Well... The reason I did not allow that is performance. You surely dont want to apply the aspect to all assemblies. Do you want to apply to mscorlib?
Gael Fraiteur, project leader
got good support? consider donating to the project.
got good support? consider donating to the project.
- gfraiteur
- Site Admin
- Posts: 674
- Joined: Tue Dec 18, 2007 3:09 pm
- Full Name: Gael Fraiteur
- Company: postsharp.org
Re: Inheritance of Aspects
gfraiteur wrote:Well... The reason I did not allow that is performance. You surely dont want to apply the aspect to all assemblies. Do you want to apply to mscorlib?
No, not at al, just the ones who reference my assembly.
I'm not complaining, just trying to find the limits of what we can do with PostSharp - of course there are bound to be some; but it just seems so magical at first, that it seems as though it can do anything.
- JeffG
- Posts: 3
- Joined: Wed Apr 16, 2008 2:58 pm
- Full Name: Jeff Goddard
- Company: RBI
11 posts • Page 1 of 2 • 1, 2