C# – Specify assembly for namespace

.net-assemblycnamespaceswpfxaml

Is there anyway to specify the assembly along with the namespace in C#?

For instance, if you reference both PresentationFramework.Aero and PresentationFramework.Luna in a project you might notice that both of them share the same controls in the same namespace but with different implementation.

Take ButtonChrome for example. It exists in both assemblies under the namespace Microsoft.Windows.Themes.

In XAML you include the assembly along with the namespace so here it's no problem

xmlns:aeroTheme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
xmlns:lunaTheme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna"

<aeroTheme:ButtonChrome ../>
<lunaTheme:ButtonChrome ../>

But in C# code behind I can't find anyway to create an instance of ButtonChrome in PresentationFramework.Aero.

The following code gives me error CS0433 when compiling

using Microsoft.Windows.Themes;
// ...
ButtonChrome buttonChrome = new ButtonChrome();

error CS0433: The type 'Microsoft.Windows.Themes.ButtonChrome' exists in both
'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\Profile\Client\PresentationFramework.Aero.dll'
and
'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\Profile\Client\PresentationFramework.Luna.dll'

Which is very understandable, the compiler has no way of knowing which ButtonChrome to choose because I haven't told it. Can I do that somehow?

Best Answer

You need to specify an alias for the assembly reference and then import via the alias:

extern alias thealias;

See the properties window for the references.

Suppose you alias the aero assembly as "aero" and the luna assembly as "luna". You could then work with both types within the same file as follows:

extern alias aero;
extern alias luna;

using lunatheme=luna::Microsoft.Windows.Themes;
using aerotheme=aero::Microsoft.Windows.Themes;

...

var lunaButtonChrome = new lunatheme.ButtonChrome();
var aeroButtonChrome = new aerotheme.ButtonChrome();

See extern alias for more info.