How can I do versioning using Custom Serialization ?
Versioning can be done using serialization as follows. [C#] //Version 2.0.0.0 [Serializable] public class MyClass : ISerializable { public int Number; public int NewField; public void GetObjectData( SerializationInfo info,StreamingContext context) { info.AddValue(‘Number’,Number); info.AddValue(‘NewField’,NewField); } protected MyClass(SerializationInfo info,StreamingContext context) { Number = info.GetInt32(‘Number’); Version storedVersion = SerializationUtil.GetVersion(info); if(storedVersion.ToString() == ‘2.0.0.0’) { NewField = info.GetInt32(‘NewField’); } else { NewField = 123;//Some default value } } public MyClass() {} } public static class SerializationUtil { static public Version GetVersion(SerializationInfo info) { string assemblyName = info.AssemblyName; /* AssemblyName is in the form of ‘MyAssembly, Version=1.2.3.4, Culture=neutral,PublicKeyToken=null’ */ char[] separators = {’,’,’=’}; string[] nameParts = assemblyName.Split(separators); return new Version(nameParts[2]); } //Rest of SerializationUtil }
How can I use MemoryStream to create a deep clone of a serializable object ?
This can be done as follows. [C#] public static class SerializationUtil { static public T Clone(T source) { Debug.Assert(typeof(T).IsSerializable); IGenericFormatter formatter = new GenericBinaryFormatter(); Stream stream = new MemoryStream(); formatter.Serialize(stream,source); stream.Seek(0,SeekOrigin.Begin); T clone = formatter.Deserialize(stream); stream.Close(); return clone; } //Rest of SerializationUtil }
How can I store a given document in a specific format using CreateSerializerWriter methods ?
You can store a given document format using CreateSerializerWriter method as follows. [C#] // Create a SerializerProvider for accessing plug-in serializers. SerializerProvider serializerProvider = new SerializerProvider(); // Locate the serializer that matches the fileName extension. SerializerDescriptor selectedPlugIn = null; foreach ( SerializerDescriptor serializerDescriptor in serializerProvider.InstalledSerializers ) { if ( serializerDescriptor.IsLoadable && fileName.EndsWith(serializerDescriptor.DefaultFileExtension) ) { // The plug-in serializer and fileName extensions match. selectedPlugIn = serializerDescriptor; break; // foreach } } // If a match for a plug-in serializer was found, // use it to output and store the document. if (selectedPlugIn != null) { Stream package = File.Create(fileName); SerializerWriter serializerWriter = serializerProvider.CreateSerializerWriter(selectedPlugIn, package); IDocumentPaginatorSource idoc = flowDocument as IDocumentPaginatorSource; serializerWriter.Write(idoc.DocumentPaginator, null); package.Close(); return true; }
How can I create Four Button controls with the contents set to a String, a DateTimeObject, an UIElement and A Panel that contains other UIElement objects?
The example Extensible Application Markup Language (XAML) version could use the <Button.Content> tags around the content of each button, but it is not necessary. The Four buttons can be created as follows. [XAML] <!–Create a Button with a string as its content.–> <Button>This is string content of a Button</Button> <!–Create a Button with a DateTime object as its content.–> <Button xmlns:sys=’clr-namespace:System;assembly=mscorlib’> <sys:DateTime>2004/3/4 13:6:55</sys:DateTime> </Button> <!–Create a Button with a single UIElement as its content.–> <Button> <Rectangle Height=’40’ Width=’40’ Fill=’Blue’/> </Button> <!–Create a Button with a panel that contains multiple objects as its content.–> <Button> <StackPanel> <Ellipse Height=’40’ Width=’40’ Fill=’Blue’/> <TextBlock TextAlignment=’Center’>Button</TextBlock> </StackPanel> </Button>
How do I enumerate child objects in a visual tree ?
WPF has a “VisualTreeHelper” class that provides the functionality to retrieve the parent objects of a visual object using the GetParent() method and child objects of a visual object using the GetChild() method by specifying the index value of the child. The following code snippet is used to enumerate all the child objects of a visual object. [C#] public void ChildEnum(Visual Visualobj) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(Visualobj); i++) { Visual childVisual = (Visual)VisualTreeHelper.GetChild(Visualobj, i); VisualEnum(childVisual); } }