What use is the Aliases property of assembly references in Visual Studio 8
This is for "extern aliases". Suppose you want to use two different types, both of which are called Foo.Bar
(i.e. Bar
in a namespace of Foo
). The two types will be in different assemblies (by definition) - you use the property in VS to associate an alias with each reference, then you can do:
extern alias FirstAlias;
extern alias SecondAlias;
using FirstBar = FirstAlias::Foo.Bar;
using SecondBar = SecondAlias::Foo.Bar;
and then use FirstBar
and SecondBar
in your code.
So basically it's an extra level of naming - and you shouldn't use it unless you really, really have to. It will confuse a lot of people. Try to avoid getting into that situation in the first place - but be aware of this solution for those times where you just can't avoid it.
The extern alias 'xxx' was not specified in a /reference option
I have the same problem and I was able to reproduce the issue.
It turns out reference aliases are ignored on projects containing xaml files which has an xmlns definition to the output assembly like xmlns:local='clr-namespace:TestProject'.
If you think this is your case as well, please vote up my bug report at Microsoft Connect.
EDIT:
There is a suggested workaround in the link above which requires editing the project file manually. In order for this to work, I had to give full path of the assembly. Add the following instructions to the end of your project file:
<Target Name="solveAliasProblem" >
<ItemGroup>
<ReferencePath Remove="FullPath.dll"/>
<ReferencePath Include="FullPath.dll">
<Aliases>ourAlias</Aliases>
</ReferencePath>
</ItemGroup>
</Target>
<PropertyGroup>
<CoreCompileDependsOn>solveAliasProblem;$(PrepareResourcesDependsOn)</CoreCompileDependsOn>
</PropertyGroup>
How to get the namespace alias operator :: to work under C#?
extern alias alias1;
using alias1::Namespace;
How to control which assembly .Net chooses in a namespace conflict?
You need to alias the reference. To do this, go to Solution Explorer and select the reference you want to alias. View the Properties and edit the Aliases field to add a unique alias name.
Once you've defined a unique alias, you edit your code to add the extern alias
declaration.
extern alias myalias;
Finally, you reference the types via the alias as in (this example aliased System.dll):
myalias::System.Diagnostics.Trace.WriteLine("I referenced this via my alias.");
This now targets the exact reference you want, even if other references also provide a type with the same name and namespace.
For additional info on aliases, see this StackOverflow answer on What use is the Aliases property of assembly references.
Specifing Alias for dll
I have done some research and what I found is giving alias to a dll from Visual studio doesnt work as expected and is reported as a bug.
http://connect.microsoft.com/VisualStudio/feedback/details/615953/reference-aliases-are-ignored-on-projects-containing-some-xaml-files.
Also the above solution only works in case code behind files and not in case of xaml. For xaml, what Marc suggested is correct with alias name given from visual studio property window as global,toolkitalias.
Hope it helps.
Reference 2 dlls using the same namespace in vb.net 3.5 project?
I'm the VB spec lead. I'm afraid that there's no VB way of doing this (short of reflection, as DaMartyr said). I know this is a drag. I'll put it on the agenda for our next VB Language Design Meeting.
Type ambiguity between two referenced assemblies
You could try the TypeForwardedTo
Attribute.
[assembly:TypeForwardedTo(typeof(Class1))]
That way you can move the type to another assembly entirely without breaking anything. You don't even need to rebuild referencing assemblies, because the runtime handles the forwarding for you.
See here for more information:
Type forwarding using TypeForwardedTo attribute
EDIT: If you can't reference Assembly2 from Assembly1, you can define an extern alias
:
MSDN Documentation for extern alias (C# Reference)
You can define them by selecting the reference to the assembly in the Solution explorer and editing the Aliases in the property Window.
You'd then just need to qualify your type with yourAlias::Class1
.
Class with same name in two assemblies (intentionally)
You can use an extern alias to reference types with the same fully qualified name from different assemblies. Select the reference to LibraryCS and update Aliases in the properties page from "global" to "LibraryCS", and add extern alias LibraryCS;
to the top of your source file, and then you can use LibraryCS::MyNamespace.MyClass
to refer to the class in LibraryCS. You can use MyNamespace.MyClass
or global::MyNamespace.MyClass
to refer to the class in LibWrapper, or you can use an alias for that reference as well.
Related Topics
String Concatenation Using '+' Operator
Has Foreach's Use of Variables Been Changed in C# 5
Open Image from File, Then Release Lock
Getfiles with Multiple Extensions
C# Using Reflection to Copy Base Class Properties
How to Correctly Cast a Class to an Abstract Class When Using Type Generics
Frombody String Parameter Is Giving Null
Method Call Using Ternary Operator
Regex Match Multiple Times in String
Releasing a Unplugged Virtual Serial Port
Bulk-Deleting in Linq to Entities
Why Doesn't C# Support the Return of References
Mvc3 Razor Dropdownlistfor Enums
How to Decode a Base64 Encoded String