How to use AttributeTargetMemberAttributes property?

Do you program directly PostSharp.CodeModel or another low-level API and need help? Put your questions here and we will try to answer.

How to use AttributeTargetMemberAttributes property?

Postby Ed_ward on Sat Dec 22, 2007 1:28 pm


Hi guys

Could anyone describe me how to use AttributeTargetMemberAttributes property?
I want some attribute to be applied only to public methods of a class.

when I specify such attribute
[ValidateArguments( AttributeTargetMemberAttributes =
                             MulticastAttributes.All |
                            ~MulticastAttributes.Private |
                            ~MulticastAttributes.Protected)]
or
[ValidateArguments( AttributeTargetMemberAttributes =
                             MulticastAttributes.AnyVisibility |
                            ~MulticastAttributes.Private |
                            ~MulticastAttributes.Protected)]
on the class definition, this attribute is still applied to private methods of this class.
Ed.ward
Edward Pavlov
Ed_ward
 
Posts: 11
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Edward Pavlov

Re: How to use AttributeTargetMemberAttributes property?

Postby gfraiteur on Sat Dec 22, 2007 6:20 pm

Ed,

It is a standard bit map where bits are grouped into the categories: visibility, virtuality, implementation, and scope.

I think what you want is:

or

[ValidateArguments( AttributeTargetMemberAttributes =

MulticastAttributes.NonAbstract |
MulticastAttributes.AnyScope |
( MulticastAttributes.AnyVisibility & ~MulticastAttributes.Private) |
MulticastAttributes.Managed )]

Why NonAbstract instead of AnyVirtuality and Managed instead of AnyImplementation? Because you derive your aspect from OnMethodBoundaryAspect, which has these limitations, and you are not allowed to enlarge the possible set of targets.

I ack there is a usability problem for this and I plan to make it easier.

Gael

Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 608
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: How to use AttributeTargetMemberAttributes property?

Postby Ed_ward on Sun Dec 23, 2007 8:32 pm

Ok, I see you point, but I want some more strange than you described :)

// attribute usages are skipped
public class ValidateArgumentsAttribute : MulticastAttribute, IRequirePostSharp
{
        public readonly Type ExceptionSpecializationType;


        public ValidateArgumentsAttribute( ) : this( null ){}
        public ValidateArgumentsAttribute( Type exceptionSpecializationType )
        {
            ExceptionSpecializationType = exceptionSpecializationType;
            AttributeReplace = true;

            AttributeTargetMemberAttributes = MulticastAttributes.NonAbstract    |                                                           MulticastAttributes.AnyScope |                                                       MulticastAttributes.Public        |
                                                      MulticastAttributes.Managed;
        }

        #region IRequirePostSharp Members
        //...
        #endregion
    }

    Please get a look at constructor of this attribute, I want to make "default" behaviour. Hence if I apply my attribute without any specifications
[ValidateArguments] I expect that it will not be applied to private methods, but I still want be able to apply this attribute to private members if I need.

    Unfortunately it does not work, but if I apply my attribute in such way (i.e. inside an AssemblyInfo)
[ValidateArguments(AttributeTargetMemberAttributes = MulticastAttributes.NonAbstract    |
                                                         
MulticastAttributes.AnyScope |
                                                     
MulticastAttributes.Public        |                                                      MulticastAttributes.Managed;)] it works as expected.

Actually, I cannot understand why it does not work...

Ed

Edward Pavlov
Ed_ward
 
Posts: 11
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Edward Pavlov

Re: How to use AttributeTargetMemberAttributes property?

Postby gfraiteur on Thu Dec 27, 2007 9:44 am

Yes, surely, it won't work. PostSharp does not execute the constructor of the custom attribute during the multicasting process. You have to set it in the CA instance.

That's why.

Any usability problem with that? I think your use case is covered by the [MulticastAttributeUsage] custom attribute.

Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 608
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: How to use AttributeTargetMemberAttributes property?

Postby Edward on Thu Feb 07, 2008 11:37 pm

Hi Gael, sorry for a very big delay :)

> Any usability problem with that? I think your use case is covered by the
> [MulticastAttributeUsage] custom attribute.

Unfortunately it isn't.
I want be able to apply my attribute to members with any visibility, but at the same time I want to have default behaviour - by default my attribute must be applied only to members with public visibility.
If I specify only public visibility in the MulticastAttributeUsage I won't be able to apply my attribute to the any private member, am I right?

Ed.ward
Edward
 
Posts: 1
Joined: Thu Feb 07, 2008 11:24 pm
Full Name: Edward Pavlov

Re: How to use AttributeTargetMemberAttributes property?

Postby gfraiteur on Fri Feb 08, 2008 1:05 am

So I think there is no solution to your problem. You cannot define a default behavior and give the possibility to override it,

Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 608
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: How to use AttributeTargetMemberAttributes property?

Postby Ed_ward on Sat Feb 09, 2008 3:59 pm

Ok, it is not a very big problem

Thanks Gael
Edward Pavlov
Ed_ward
 
Posts: 11
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Edward Pavlov


Return to PostSharp Core