Retrieving custom attribute info on properties?

B

Ben Fidge

I've created a custom attribute that is applied to class properties (uses
AttributeTargets.Property).

How do I read a given property's attributes at runtime to check for the
existence of my custom attribute? Ideally, I'd like to write a generic
function that will perform this check from within a property's "get"
accessor and return a boolean denoting the custom attributes existence.

How would I pass a property name, not it's value, to a function and then
read of that property's attributes?

Example

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

public class MyClass {
private bool CheckForAttribute(??) {
// Check passed in property's Attributes to see if there is a
MyAttribute
}

[MyAttribute]
public string SomeValue {
get {
if (! CheckForAttribute(??)) throw new Exception("Attribute does
not exist");
}
}
}

Thanks

Ben
 
M

Mattias Sjögren

Ben,
How would I pass a property name, not it's value, to a function and then
read of that property's attributes?

As a string; "SomeValue". You then use reflection (Type.GetProperty)
to read its attributes.



Mattias
 
A

Andreas Håkansson

Ben,

Could you live with having to pass the name of the property in
question to the check function? If not, then you could use something
like this (note this is untested code, written directly in this post and
has not been compiled and executed).

public bool CheckForAttribute(Type type, string property)
{
PropertyInfo propInfo =
type.GetProperty(property);

if( propInfo == null)
throw ArgumentException("Unable to find the specified property",
"property");

MyAttribute[] attributes =
(MyAttribute[])propInfo.GetCustomAttributes(typeof(MyAttribute),
true);

return (attributes.Length > 0);
}

Then you could do

[MyAttribute]
public string SomeValue
{
get
{
if( !CheckForAttribute(this, "SomeValue") )
throw new Exception("Attribute does not exist");
}
}

You could extend the method to accept a second type, which would
be the type of attribute you would like to check for if you wanted to
make a more generic routine which you could reuse in other code.

Hope this helps,

//Andreas Håkansson
 
B

Ben Fidge

That simple! It's a shame theres no way to directly pass a reference to the
property, without using strings as if I change the property name later on
and forget to update the string, the compiler won't catch it.

Thanks,

Ben
 
B

Ben Fidge

Thanks for the sample code. As I said in my reply to Mattias, in this
thread, it's a shame that it can't be done without using strings as this
leaves scope for error if property names get changed later on.

Thanks for your time,

Ben


Andreas Håkansson said:
Ben,

Could you live with having to pass the name of the property in
question to the check function? If not, then you could use something
like this (note this is untested code, written directly in this post and
has not been compiled and executed).

public bool CheckForAttribute(Type type, string property)
{
PropertyInfo propInfo =
type.GetProperty(property);

if( propInfo == null)
throw ArgumentException("Unable to find the specified property",
"property");

MyAttribute[] attributes =
(MyAttribute[])propInfo.GetCustomAttributes(typeof(MyAttribute),
true);

return (attributes.Length > 0);
}

Then you could do

[MyAttribute]
public string SomeValue
{
get
{
if( !CheckForAttribute(this, "SomeValue") )
throw new Exception("Attribute does not exist");
}
}

You could extend the method to accept a second type, which would
be the type of attribute you would like to check for if you wanted to
make a more generic routine which you could reuse in other code.

Hope this helps,

//Andreas Håkansson


Ben Fidge said:
I've created a custom attribute that is applied to class properties (uses
AttributeTargets.Property).

How do I read a given property's attributes at runtime to check for the
existence of my custom attribute? Ideally, I'd like to write a generic
function that will perform this check from within a property's "get"
accessor and return a boolean denoting the custom attributes existence.

How would I pass a property name, not it's value, to a function and then
read of that property's attributes?

Example

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

public class MyClass {
private bool CheckForAttribute(??) {
// Check passed in property's Attributes to see if there is a
MyAttribute
}

[MyAttribute]
public string SomeValue {
get {
if (! CheckForAttribute(??)) throw new Exception("Attribute does
not exist");
}
}
}

Thanks

Ben
 
A

Andreas Håkansson

Ben,

Yes, I agree, it would have been nice if the GetProperty() method had an
overload
which enabled you to pass it this like GetProperty(this).

//Andreas

Ben Fidge said:
Thanks for the sample code. As I said in my reply to Mattias, in this
thread, it's a shame that it can't be done without using strings as this
leaves scope for error if property names get changed later on.

Thanks for your time,

Ben


Andreas Håkansson said:
Ben,

Could you live with having to pass the name of the property in
question to the check function? If not, then you could use something
like this (note this is untested code, written directly in this post and
has not been compiled and executed).

public bool CheckForAttribute(Type type, string property)
{
PropertyInfo propInfo =
type.GetProperty(property);

if( propInfo == null)
throw ArgumentException("Unable to find the specified property",
"property");

MyAttribute[] attributes =
(MyAttribute[])propInfo.GetCustomAttributes(typeof(MyAttribute),
true);

return (attributes.Length > 0);
}

Then you could do

[MyAttribute]
public string SomeValue
{
get
{
if( !CheckForAttribute(this, "SomeValue") )
throw new Exception("Attribute does not exist");
}
}

You could extend the method to accept a second type, which would
be the type of attribute you would like to check for if you wanted to
make a more generic routine which you could reuse in other code.

Hope this helps,

//Andreas Håkansson


Ben Fidge said:
I've created a custom attribute that is applied to class properties (uses
AttributeTargets.Property).

How do I read a given property's attributes at runtime to check for the
existence of my custom attribute? Ideally, I'd like to write a generic
function that will perform this check from within a property's "get"
accessor and return a boolean denoting the custom attributes existence.

How would I pass a property name, not it's value, to a function and then
read of that property's attributes?

Example

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

public class MyClass {
private bool CheckForAttribute(??) {
// Check passed in property's Attributes to see if there is a
MyAttribute
}

[MyAttribute]
public string SomeValue {
get {
if (! CheckForAttribute(??)) throw new
Exception("Attribute
does
not exist");
}
}
}

Thanks

Ben
 

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

Top