Specify the assembly for the namespace

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

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

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

In XAML, you enable assembly along with the namespace, so this is not a 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 the C # code behind, I still can't 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 as in '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 clear, the compiler does not know which ButtonChrome to choose, because I did not say that. Can I do it somehow?

+7
source share
3 answers

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

 extern alias thealias; 

See the properties window for links.

Suppose you look like an aero assembly like aero, and a moon like a moon. Then you can work with both types in a single 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 the alias extern for details.

+7
source

Hidden alias for salvation, see documentation here . After adding assembly links and creating the aliases Luna and Aero in the corresponding reference properties, here is an example of code that 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); } } } 
+5
source

I encountered a similar error regarding System.NonSerializedAttribute when referring to the Microsoft.Scripting assembly, which also defines this attribute (a collision is found in the Reference.cs file generated by the service link). The easiest way to solve this problem is very similar to defining aliases, but without compiling a headache:

In Visual Studio, go to the links of your project, select one of the collectors that create the conflict, go to the properties and fill the "Aliases" value with a value that is not equal to global .

0
source

All Articles