Retrieving custom attribute info on properties?

  • Thread starter Thread starter Ben Fidge
  • Start date Start date
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
 
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
 
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
 
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
 
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
 
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
 
Back
Top