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?
Right click the website/web application property and set Assembly name field with the intended name. Eg: If the application is named as XYZ and uses namespace naming as companyname. dept. XYZ then the application may have 2 separate dlls.
Namespaces organize the objects defined in an assembly. Assemblies can contain multiple namespaces, which can in turn contain other namespaces. Namespaces prevent ambiguity and simplify references when using large groups of objects such as class libraries.
In brief, a namespace is a logical group of related classes that can be used by the languages targeted by Microsoft . NET framework, while an assembly is a building block of . NET Framework applications that form the fundamental unit of deployment, version control, reuse, activation scoping, and security permissions.
Namespace: System.Reflection. Summary. Defines an Assembly, which is a reusable, versionable, and self-describing building block of a common language runtime application. C# Syntax: [Serializable]
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.
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With