The PropertyChangedAttribute
was processed by PostSharp. But I found a work-around.
My DependsOnAttribute
looks like this:
[Serializable]
public sealed class DependsOnAttribute : Attribute
{
private readonly string[] _properties;
public DependsOnAttribute(params string[] properties)
{
_properties = properties;
}
public IEnumerable<string> Properties
{
[DebuggerStepThrough]
get { return _properties; }
}
}
As you can see, all it does is collect a list of strings.
I modified my ChangeNotifierAttribute
. I know that Entity implements PropertyChanged
, so all I needed was the name of the function used to raise the event. I found this in another question on this support board.
[Serializable]
[MulticastAttributeUsage(MulticastTargets.Class, Inheritance = MulticastInheritance.None)]
public sealed class ChangeNotifierAttribute : InstanceLevelAspect
{
[ImportMember("NotifyPropertyChanged", IsRequired = true)] public Action<string> OnRaisePropertyChanged;
private object _instance;
public override void RuntimeInitializeInstance()
{
Type type = _instance.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
object[] attributes = property.GetCustomAttributes(typeof (DependsOnAttribute), true);
foreach (object att in attributes)
{
if (att.GetType() == typeof (DependsOnAttribute))
{
DependsOnAttribute doa = att as DependsOnAttribute;
if (doa != null)
{
Entity entity = _instance as Entity;
if (entity != null)
{
PropertyInfo info = property;
entity.PropertyChanged += (o, e) =>
{
if (doa.Properties.Contains(e.PropertyName))
{
OnRaisePropertyChanged.Invoke(info.Name);
}
};
}
}
}
}
}
base.RuntimeInitializeInstance();
}
public override object CreateInstance(AdviceArgs adviceArgs)
{
_instance = adviceArgs.Instance;
return base.CreateInstance(adviceArgs);
}
}
For some strangeness with PostSharp's InstanceLevelAspect
functionality I needed to introduce the member, otherwise the overrides were not called.
Now I do no longer need the PropertyChangedAttribute
, as Field already does this, the other aspects remain in place.
answered
Mar 25 '11 at 05:55
jensen
39●9●9●13
I'm afraid that there is no such an attribute for dependencies.
As for
PropertyChanged
attribute — could you check whether the aspect is being processed by PostSharp or not?