J
jehugaleahsa
Hello:
I would like code that will return all properties that have a
specified attribute type.
And, once that is done, I would like code that returned all properties
that have an attribute that had a property value equal to a given
value.
In my case, I have a bunch of attributes associated with properties in
my business objects that specify the database column name that the
property works with. I would like to implement INotifyPropertyChanged
generically in an abstract base class called ABusinessObject.
ABusinessObject is responsible for taking the database column name and
updating a data store. When it does, I would like it to raise the
PropertyChanged event. However, PropertyChanged event is expected to
return the property name, not the column name.
Now, since my attributes hold the database column names for the
associated property, I should be able to look at my
"ABusinessObject.GetType().GetProperties()
[0..*].Attributes[0..*].ColumnName" and property with an attribute
with the column name I am given.
Pretty crazy, huh?
Anyway, below is the gross code (it has a goto in it) for finding what
I am looking for. I was hoping someone knew a better way.
if (propertyChanged != null)
{
Type businessObjectType = this.GetType();
string propertyName = columnName; // use as a default
foreach (PropertyInfo property in
businessObjectType.GetProperties())
{
foreach (ColumnInformationAttribute attribute
in
property.GetCustomAttributes(typeof(ColumnInformationAttribute),
true))
{
if (attribute.Name == columnName)
{
propertyName = property.Name;
goto found;
}
}
}
found:
propertyChanged(this, new
PropertyChangedEventArgs(propertyName));
}
Thanks,
Travis
I would like code that will return all properties that have a
specified attribute type.
And, once that is done, I would like code that returned all properties
that have an attribute that had a property value equal to a given
value.
In my case, I have a bunch of attributes associated with properties in
my business objects that specify the database column name that the
property works with. I would like to implement INotifyPropertyChanged
generically in an abstract base class called ABusinessObject.
ABusinessObject is responsible for taking the database column name and
updating a data store. When it does, I would like it to raise the
PropertyChanged event. However, PropertyChanged event is expected to
return the property name, not the column name.
Now, since my attributes hold the database column names for the
associated property, I should be able to look at my
"ABusinessObject.GetType().GetProperties()
[0..*].Attributes[0..*].ColumnName" and property with an attribute
with the column name I am given.
Pretty crazy, huh?
Anyway, below is the gross code (it has a goto in it) for finding what
I am looking for. I was hoping someone knew a better way.
if (propertyChanged != null)
{
Type businessObjectType = this.GetType();
string propertyName = columnName; // use as a default
foreach (PropertyInfo property in
businessObjectType.GetProperties())
{
foreach (ColumnInformationAttribute attribute
in
property.GetCustomAttributes(typeof(ColumnInformationAttribute),
true))
{
if (attribute.Name == columnName)
{
propertyName = property.Name;
goto found;
}
}
}
found:
propertyChanged(this, new
PropertyChangedEventArgs(propertyName));
}
Thanks,
Travis