Monday, January 12, 2009

How to assign a URL to one or more CLR namespaces

Look at the way the XML namespaces are defined in the snippet below. The 'local' definition is mapped directly to a CLR namespace in the normal way but the first two definitions are just URLs. What do thes URLs represent and where are they defined?
<Window x:Class="WpfLibrary.Window1"
        <SolidColorBrush Color="AliceBlue"/>

The URLs have been setup to represent multiple CLR namespaces in the AssemblyInfo of the assembly in which they are defined. Notice that both the TextBox and SolidColorBrush classes are referenced using the default XML namespace even though the classes are actually defined in different CLR namespaces.

To find out which CLR namespaces are assigned to a particular URL then you can look at the assembly definition in Reflector.

Here's the syntax to set this up in the AssemblyInfo of your assembly:
[assembly:XmlnsDefinition("", "WpfLibrary")]
[assembly:XmlnsDefinition("", "WpfLibrary.Pens")]
[assembly:XmlnsDefinition("", "WpfLibrary.Brushes")]
Consumers of your assembly can then use the XML namespace to reference all of the associated CLR namespaces in one swoop:
<Window x:Class="WpfApplication1.Window1"

No comments: