March 24, 2006
02:29 PM

Joseph recently posted How can I decompile/extract a WPF Control's default template as Xaml? on learnwpf.com. I need to do this often so I wrote a simple command line application.

using System;

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        if (args.Length < 1)
            Console.WriteLine("Usage: extractXAML.exe (type) [property]");

        object obj = Activator.CreateInstance(Type.GetType(args[0]));

        System.Windows.Markup.IAddChild window = new System.Windows.Window();
        window.AddChild(obj);

        object value = (args.Length > 1) ? obj.GetType().GetProperty(args[1]).GetValue(obj, null) : obj;

        System.Windows.Markup.XamlWriter.Save(value, System.Console.Out);
    }
}

I'm only using Type.GetType(string) so you need to specify the Assembly Qualified Name.

Usage example: extractXAML "System.Windows.Controls.TreeView, PresentationFramework, Version=3.0.51116.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" Template

© Douglas Stockwell 2007
Creative Commons License Unless otherwise specified all "source code" examples are available for use under the Creative Commons Attribution-Noncommercial 3.0 License. Please contact me if you would like more flexible licensing terms.
Messenger Presence