3

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

Zefo
  • 217
  • 1
  • 3
  • 7

3 Answers3

3

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.

Drew Marsh
  • 33,111
  • 3
  • 82
  • 100
3

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...)

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
1

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:

Dan Stevens
  • 6,392
  • 10
  • 49
  • 68
Tim Lovell-Smith
  • 15,310
  • 14
  • 76
  • 93