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
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