question about custom attributes

  • Thread starter Thread starter M
  • Start date Start date
M

M

Is it possible to define an attribute that describes a property of a
class at runtime?

For example, you have a class called "employee", and it has a property
"name". The "name" property has an attribute that tells any calling
code whether or not this property is "writable" and/or "readable", even
though this property has both get/set accessors.

Maybe there is another way to do this, and I just don't have enough
experience.

Any help is appreciated.
 
Is the following possible? If so, is this the proper way to do it?

[AttributeUsage(AttributeTarget.Property,Inherited=true,AllowMultiple=true)]

public class SecurityInfoAttribute : Attribute
{
// Private Data
private int readable;
private int writable;


// Constructor
public SecurityInfoAttribute(int readable, int writable)
{
this.security_readable = 0;
this.security_writable = 0;

}

public int Readable
{
get
{
return readable;
}
set
{
readable = value;
}
}

public int Writable
{
get
{
return writable;
}
set
{
writable = value;
}
}


}



public class employee {

string name;
[SecurityInfo(getReadSecurityInfo(userId), getWriteSecurityInfo(userId))]
string Name{get{return name;}set{name = value;}}



}

And then examine the name property of this object from within the client
code in order to show it to that user or not, based on the security info
attribute.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top