Connecting Tech Pros Worldwide Help | Site Map

Some attribute questions

Newbie
 
Join Date: Oct 2008
Posts: 8
#1: Oct 1 '08
Hello.

Can i specify that an attribute can only be applied to a property with a set accessor?

Can i get all attributes of a specific type of a class and then through the attribute instance get the members that they where applied to?

Thanks in advance.
vanc's Avatar
Expert
 
Join Date: Mar 2007
Posts: 202
#2: Oct 1 '08

re: Some attribute questions


I'm not fully understand what you're asking about. If you can revise your question, I may help you out here. If you can also throw in some pseudo codes, that's great to illustrate your idea. Cheers.
Newbie
 
Join Date: Oct 2008
Posts: 8
#3: Oct 1 '08

re: Some attribute questions


what i want to know is if there is a way to specify to the compiler that a custom attribute can only be applied to a property with a set accessor (CanWrite == true).

Something like AttributeUsage does.

[AttributeUsage(AttributeTargets.Property)]
public class SomeAttribute : Attribute {}

public SomeClass
{
[SomeAttribute ] //I want a compiler error here because the attribute SomeAttribute is applied to an property that does not have a set
public int onlyGetProp { get {return 0;} }

[SomeAttribute ] //
public int onlyGetProp { get {return 0;} set { /*do something*/} }
}

Is it possible to have the described behavior?

Also can i do something like this:

Attribute[] atts = (Attribute[])GetType().GetCustomAttributes(typeof(SomeAttribut e), true);
foreach(Attribute a in atts)
{
MemberInfo mi = a.getMember(); //this method should give the member info where the instance of the attribute has applied, does such thing exists???
}
Reply