如何序列化附加属性

发布于 2024-08-08 15:53:10 字数 379 浏览 16 评论 0原文

我正在使用复合应用程序库编写.NET3.5、WPF 应用程序。应用程序分为几个模块。

在基础设施模块中,我定义了 NetworkNode 对象。 Network 模块管理 NetworkNode 的集合,并使用 XmlSerializer 来存储/加载该集合。到目前为止一切正常。

但我还有其他模块,例如 NodeModule。如果在网络模块中选择了网络节点,则使用 E​​ventAggregator 将事件发布到其他模块。这些模块可以使用附加属性将各种信息附加到 NetworkNode。

问题是 NetworkModule 不知道其他模块,因此这些属性没有序列化。是否可以以某种方式列出并序列化附加到对象的所有属性?或者我是否必须改变概念并使用附加属性以外的其他东西?

问候

I am writing .NET3.5, WPF application using Composite Application Library. Application is divided into several modules.

In infrastructure module I have defined NetworkNode object. The Network module manages a collection of NetworkNodes and uses XmlSerializer to store/load this collection. So far everythings works.

But I have other modules e.g NodeModule. If a NetworkNode was selected in Network module, an event is published to other modules using EventAggregator. These modules can attach various information to the NetworkNode using attached properties.

The problem is the NetworkModule does not know about the other modules, therefor these properties are not serialized. It is possible to somehow list and serialize all properties attached to an object? Or do I have to change the concept and use something else than attached properties?

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

熊抱啵儿 2024-08-15 15:53:10

您可以使用 DependencyObject.GetLocalValueEnumerator

    LocalValueEnumerator propEnumerator = foo.GetLocalValueEnumerator();
    while (propEnumerator.MoveNext())
    {
        Console.WriteLine ("{0} = {1}",
                           propEnumerator.Current.Property.Name,
                           propEnumerator.Current.Value);
    }

但是,这对 XML 序列化没有帮助(除非您实现 IXmlSerialized,这很痛苦......)。您可能应该使用 XamlWriter 相反(我认为这就是 Drew 所说的,因为没有 XamlSerializer...)

You can list all dependency properties (attached or not) defined on an object using DependencyObject.GetLocalValueEnumerator :

    LocalValueEnumerator propEnumerator = foo.GetLocalValueEnumerator();
    while (propEnumerator.MoveNext())
    {
        Console.WriteLine ("{0} = {1}",
                           propEnumerator.Current.Property.Name,
                           propEnumerator.Current.Value);
    }

However, this won't help for XML serialization (unless you implement IXmlSerializable, which is a pain...). You should probably use XamlWriter instead (which I assume is what Drew was talking about, since there is no XamlSerializer...)

月下客 2024-08-15 15:53:10

由于附加属性从纯 CLR 角度来看是不可见的,因此 XmlSerializer 无法了解它们。您需要切换到使用 XamlSerializer 体系结构,以便能够序列化“普通”CLR 对象并拥有 DependencyObjects 的特殊知识。

Since attached properties aren't visible from a pure CLR perspective, the XmlSerializer has no way to know about them. You would need to switch to use the XamlSerializer architecture in order to be able to serialize both "plain" CLR objects as well as have the special knowledge of DependencyObjects.

凉薄对峙 2024-08-15 15:53:10

如果您使用的是.Net 4.0(我相信它们不在.Net 3.5中)

您可以使用 IAttachedPropertyStoreAttachablePropertyServices

参考示例 #1:.Net 4.0 中的 XAML :附加属性、IAttachedPropertyStore 和 AttachablePropertyServices

另外,一般来说,附加属性必须正确定义:

  1. 它必须是公共(或在某些情况下为内部工作)非嵌套类型的属性(即,它不是在另一个内部声明的) type), T.
  2. 定义一个新的 AttachableMemberIdentifier(T, "MyProperty")
  3. 在 T 上提供名为“SetMyProperty”和“GetMyProperty”的公共静态方法,即“MyProperty”部分必须与您的 AttachableMemberIdentifier 匹配。 (您不能使用“Foo”作为 AttachableMemberIdentifier 中的名称并将它们称为“SetBar”和“GetBar”,因为 Xaml 序列化程序堆栈需要通过反射找到它们。)然后,这些方法应利用 AttachablePropertyServices 来存储附加的属性值。

参考示例#2:

If you are using .Net 4.0 (I believe they aren't in .Net 3.5)

You can use either IAttachedPropertyStore or AttachablePropertyServices

Reference Example #1: XAML in .Net 4.0: Attached Properties, IAttachedPropertyStore and AttachablePropertyServices

Also, generally, the attached property must be defined correctly:

  1. It must be a property of public (or internal works in some scenarios) non-nested type (i.e. it is not declared inside another type), T.
  2. Define a new AttachableMemberIdentifier(T, "MyProperty")
  3. Provide public static methods on T called "SetMyProperty" and "GetMyProperty", i.e. the "MyProperty" part must match your AttachableMemberIdentifier. (You can't use "Foo" as the name in the AttachableMemberIdentifier and call them "SetBar" and "GetBar" because the Xaml serializer stack needs to find them by reflection.) These methods should then leverage AttachablePropertyServices to store the attached property value.

Reference Example #2:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文