Im new to .NET 및 WPF 그래서 나는 질문을 올바르게 요청할 것입니다. 나는에서 INotifyPropertyChanged이 PostSharp 1.5을 사용하여 구현 사용하고 있습니다 :PostSharp 1.5로 INotifyPropertyChanged 구현
[Serializable, DebuggerNonUserCode, AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class, AllowMultiple = false, Inherited = false),
MulticastAttributeUsage(MulticastTargets.Class, AllowMultiple = false, Inheritance = MulticastInheritance.None, AllowExternalAssemblies = true)]
public sealed class NotifyPropertyChangedAttribute : CompoundAspect
{
public int AspectPriority { get; set; }
public override void ProvideAspects(object element, LaosReflectionAspectCollection collection)
{
Type targetType = (Type)element;
collection.AddAspect(targetType, new PropertyChangedAspect { AspectPriority = AspectPriority });
foreach (var info in targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(pi => pi.GetSetMethod() != null))
{
collection.AddAspect(info.GetSetMethod(), new NotifyPropertyChangedAspect(info.Name) { AspectPriority = AspectPriority });
}
}
}
[Serializable]
internal sealed class PropertyChangedAspect : CompositionAspect
{
public override object CreateImplementationObject(InstanceBoundLaosEventArgs eventArgs)
{
return new PropertyChangedImpl(eventArgs.Instance);
}
public override Type GetPublicInterface(Type containerType)
{
return typeof(INotifyPropertyChanged);
}
public override CompositionAspectOptions GetOptions()
{
return CompositionAspectOptions.GenerateImplementationAccessor;
}
}
[Serializable]
internal sealed class NotifyPropertyChangedAspect : OnMethodBoundaryAspect
{
private readonly string _propertyName;
public NotifyPropertyChangedAspect(string propertyName)
{
if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException("propertyName");
_propertyName = propertyName;
}
public override void OnEntry(MethodExecutionEventArgs eventArgs)
{
var targetType = eventArgs.Instance.GetType();
var setSetMethod = targetType.GetProperty(_propertyName);
if (setSetMethod == null) throw new AccessViolationException();
var oldValue = setSetMethod.GetValue(eventArgs.Instance, null);
var newValue = eventArgs.GetReadOnlyArgumentArray()[0];
if (oldValue == newValue) eventArgs.FlowBehavior = FlowBehavior.Return;
}
public override void OnSuccess(MethodExecutionEventArgs eventArgs)
{
var instance = eventArgs.Instance as IComposed<INotifyPropertyChanged>;
var imp = instance.GetImplementation(eventArgs.InstanceCredentials) as PropertyChangedImpl;
imp.OnPropertyChanged(_propertyName);
}
}
[Serializable]
internal sealed class PropertyChangedImpl : INotifyPropertyChanged
{
private readonly object _instance;
public PropertyChangedImpl(object instance)
{
if (instance == null) throw new ArgumentNullException("instance");
_instance = instance;
}
public event PropertyChangedEventHandler PropertyChanged;
internal void OnPropertyChanged(string propertyName)
{
if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException("propertyName");
var handler = PropertyChanged as PropertyChangedEventHandler;
if (handler != null) handler(_instance, new PropertyChangedEventArgs(propertyName));
}
}
}
그럼 난 [NotifyPropertyChanged] 구현하는 클래스 (사용자 및 ADRESS)의 몇 가지있다. 잘 작동합니다. 하지만 내가 원하는 것은 자식 개체가 (내 예제 주소에서) 부모 개체가 알림을 받았다면 (내 경우 사용자)입니다. 이 코드를 확장하여 자식 객체의 변경 사항을 수신하는 부모 객체의 리스너를 자동으로 만들 수 있습니까?
아동 청취자는 무엇을하고 싶습니까? –
사실이 시점에서 내가 원하는 것은 학부모가 통지를 받는다는 것입니다 (아동의 변화에 따라 - 깊이가 있음). – no9
그것은 훨씬 더 어려운 문제입니다.일종의 반성을해야 할 것입니다. 자녀에게 자녀의 변화를 알리기 위해 의존 할 수 없다면, 반성하는 동안 언제, 어떻게 언제 재귀 할 것인지를 결정할 때가 있습니다. 이 솔루션에 당신을 이끄는 동기 부여 문제는 무엇입니까? 작업을 단순화하는 데 도움이 될 수있는 디자인 변경이있을 수 있습니다. –