gpt4 book ai didi

c# - 跨序列化对象反射以设置 PropertyChanged 事件

转载 作者:行者123 更新时间:2023-11-30 12:15:19 26 4
gpt4 key购买 nike

我有一个通过反序列化某些 XML 创建的对象。我使用 Visual Studio 的工具从供应商模型 XML 生成 XSD。然后使用 XSD 工具我从他们那里得到了类。我设置了 XSD 工具,使生成的类成为 INotifyPropertyChanged。

现在我试图在我的 WPF 应用程序的“选项卡”中显示此对象。每当有人进行更改时,我都想要一个“脏”指示器。问题是这个对象,是从生成的 XSD 生成的类,并不是最漂亮的结构。此对象的显示不模仿其数据结构。我想创建显示对象(不使用 MMVM ATM)并使用它们将我的更改绑定(bind)到,然后将这些更改作为数据对象保存到对象。我基本上只是扔掉我当前的显示来执行此操作,因为我现在只是添加一个检查是否有编辑。

我的想法是反射(reflect)整个对象并为我遇到的每个属性设置 PropertyChanged 事件(遍历对象和属性的图表)。我的反射(reflection)失败了,我也可能犯了一些短视的错误。

这是我到目前为止编写的代码:

void SetupPropertyChanged(INotifyPropertyChanged component)
{
component.PropertyChanged += CAMConfig_PropertyChanged;

Type componentType = component.GetType();

foreach (PropertyInfo info in componentType.GetProperties())
{
Type[] types =
info.PropertyType.FindInterfaces((a, b) => { return a.ToString() == b.ToString(); }, typeof(INotifyPropertyChanged));

bool isINotify = types.Contains(typeof(INotifyPropertyChanged));


if (isINotify)
this.SetupPropertyChanged((INotifyPropertyChanged)info.GetValue(component, new object[] { }));
}
}

我想我遇到了 Observable 集合属性类型问题,因为它在我遍历我的对象时抛出了异常。我还突然想到,我不知道这个对象结构是否会有循环引用。

谁能帮我编写这段代码,以便我可以遍历对象图。现在我不太担心循环引用的可能性,但如果出现一种解决方案可以防止这种情况发生,那将非常非常有帮助!

根据 Karel 的回答,我创建了这个“助手”类:

    public static class NotifyPropertyChangedHelper
{
public delegate void ChangeOccuredHandler(object sender);

public static void SetupPropertyChanged(INotifyPropertyChanged component, ChangeOccuredHandler changedHandler)
{
SetupPropertyChanged(new List<object>(), component, changedHandler);
}

static void SetupPropertyChanged(IList<object> closed, INotifyPropertyChanged component, ChangeOccuredHandler changedHandler)
{
if (closed.Contains(component)) return; // event was already registered

closed.Add(component); //adds the property that is to be processed

//sets the property changed event if the property isn't a collection
if (!(component is INotifyCollectionChanged))
component.PropertyChanged += (sender, e) =>
{
changedHandler(sender);
};

/*
* If the component is an enumerable there are two steps. First check to see if it supports the INotifyCollectionChanged event.
* If it supports it add and handler on to this object to support notification. Next iterate through the collection of objects
* to add hook up their PropertyChangedEvent.
*
* If the component isn't a collection then iterate through its properties and attach the changed handler to the properties.
*/
if (component is IEnumerable<object>)
{
if (component is INotifyCollectionChanged)
{
//((INotifyCollectionChanged)component).CollectionChanged += collectionHandler;
((INotifyCollectionChanged)component).CollectionChanged += (sender, e) =>
{
changedHandler(sender);
};
}

foreach (object obj in component as IEnumerable<object>)
{
if (obj is INotifyPropertyChanged)
SetupPropertyChanged(closed, (INotifyPropertyChanged)obj, changedHandler);
}
}
else
{
foreach (PropertyInfo info in component.GetType().GetProperties())
{
var propertyValue = info.GetValue(component, new object[] { });
var inpc = propertyValue as INotifyPropertyChanged;
if (inpc == null) continue;
SetupPropertyChanged(closed, inpc, changedHandler);
}
}
}
}

最佳答案

保留一个您已经为其注册事件的属性(组件)列表,并使您的函数递归。您可以将组件直接转换为 INotifyPropertyChanged

void SetupPropertyChanged(IList<object> closed, INotifyPropertyChanged component)  
{
if(closed.Contains(component)) return; // event was already registered

closed.Add(component);

component.PropertyChanged += CAMConfig_PropertyChanged;
foreach (PropertyInfo info in componentType.GetProperties())
{
var propertyValue = info.GetValue(component, new object[] { });
var inpc = propertyValue as INotifyPropertyChanged;
if(inpc == null) continue;
this.SetupPropertyChanged(closed, inpc);
}
}

关于c# - 跨序列化对象反射以设置 PropertyChanged 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7597761/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com