12
votes

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?

3

3 Answers

10
votes

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.

6
votes

Extern alias to the rescue, see documentation here. Having added the assembly references and created the aliases Luna and Aero in the respective reference properties, here is some sample code you can try:

extern alias Aero;
extern alias Luna;

using System.Windows;

namespace WpfApplication1
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow: Window
  {
    public MainWindow()
    {
      InitializeComponent();

      var chrome1 = new Luna::Microsoft.Windows.Themes.ButtonChrome();
      var chrome2 = new Aero::Microsoft.Windows.Themes.ButtonChrome();
      MessageBox.Show(chrome1.GetType().AssemblyQualifiedName);
      MessageBox.Show(chrome2.GetType().AssemblyQualifiedName);
    }
  }
}
1
votes

I have met a similar error regarding System.NonSerializedAttribute when referencing Microsoft.Scripting assembly, which also defines this attribute (clash found in Reference.cs file generated by service reference). The easiest way to solve this is very similar to defining aliases, but without the compilation headache:

In Visual Studio, go to the references of your project, select one of the assemblies generating the conflict, go to properties and fill Aliases value with something not equal to global.