Click here to Skip to main content
Licence Ms-PL
First Posted 25 Feb 2010
Views 4,415
Bookmarked 0 times

Serialize DependencyObject: It's easy !

By | 25 Feb 2010 | Technical Blog
An easy way to perform serialization of these objects
A Technical Blog article. View original blog here.[^]

You often read on the web that the DependencyObjects are not marked as serializable and that this is a major drawback...

But there is an easy way to perform serialization of these objects: use XAMLWriter and XAMLReader:

public class MyDependencyObject : DependencyObject
{
public static readonly DependencyProperty MyDependencyPropertyProperty =
  DependencyProperty.Register("MyDependencyProperty", 
	typeof(String), typeof(MyDependencyObject));
 
  public String MyDependencyProperty
  {
    get { return (String)GetValue(MyDependencyObject.MyDependencyPropertyProperty); }
    set { SetValue(MyDependencyObject.MyDependencyPropertyProperty, value); }
  }
 
}
 
...
 
MyDependencyObject obj = new MyDependencyObject();
obj.MyDependencyProperty = "One love";
String serializedString = XamlWriter.Save(obj);
Console.WriteLine(serializedString);

It will produce something like this:

<MyDependencyObject MyDependencyProperty="One love" 
	xmlns="clr-namespace:ANTOINE.Jonathan;assembly=ANTOINE.Jonathan" />
Shout it kick it on DotNetKicks.com

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

jmix90

Software Developer
http://wpf-france.fr
France (Metropolitan) France (Metropolitan)

Member

Follow on Twitter Follow on Twitter
Jonathan creates software, mostly with C#,WPF and XAML.

He really likes to works on every Natural User Interfaces(NUI : multitouch, touchless, etc...) issues.



He is awarded Microsoft MVP in the "Client Application Development" section since 2011.
 

You can check out his WPF/C#/NUI/3D blog http://www.jonathanantoine.com.

He is also the creator of the WPF French community web site : http://wpf-france.fr.

Here is some videos of the projects he has already work on :


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionAny easy solution for generics too? Pinmemberhans windhoff18:17 30 Sep '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 25 Feb 2010
Article Copyright 2010 by jmix90
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid