Two Different Dll with Same Namespace

two different DLL with same namespace

There's nothing special you need to do - just reference them and use the types. Namespaces can span accross several assemblies without problems, because they're not really opaque types. A namespace is just a way of adding a common prefix to all the types it contains, allowing you to have multiple types of the same name under different namespaces. (The framework doesn't see them as having the same names, because it sees the "fully qualified" name of everything - which has an alias and a namespace attached to the front of it.)

In the rare event that you reference 2 assemblies which have the same type names and the same namespaces (such as 2 different versions of the same dll) - you can distinguish which assembly to use for a given type using an alias. The default alias for all references is global, but you can specify your own alias for any assembly when you reference it (using a compiler switch - or just use the properties box in Visual Studio) - and have an extern alias <name> clause at the top of your code file where you use it - you would access the types from different assemblies with <name>::MyNamespace.Type

Using multiple dll-s with same namespace

I made wrapper around old one and included it as OldInterop with that namespace.

.NET - Multiple libraries with the same namespace - referencing

You need to use an extern alias - introduced in C# 2.

Anson Horton has a walkthrough on his blog which you may find useful.

ambiguous class with namespace names in 2 dlls

Let's suppose that you have 2 assemblies (ClassLibrary1.dll and ClassLibrary2.dll) that both define the same class in the same namespace:

namespace Foo
{
public class Bar
{
}
}

Now in the consuming project you could define an additional alias in the references of the class library:

Sample Image

And now you could do the following to help the compiler disambiguate:

extern alias lib1;
extern alias lib2;

class Program
{
static void Main()
{
var barFromLib1 = new lib1::Foo.Bar();
var barFromLib2 = new lib2::Foo.Bar();
}
}

Reference Two DLLs with the same namespaces and types

Sorry, I had hoped to paste this a comment, but SO prevents me from doing so.

As far as I know, VB has not added the C# Aliasing feature, but your assertion that there is no solution in VB.Net is incorrect.

Your referenced post from 2011 points you to using Reflection as a workaround. I think the simplest route would be to choose which DLL you want to have Intellisense support for and add a reference to that DLL. Then you can use Reflection.Assembly.LoadFile to get a reference to the second DLL and use the CreateInstance method on that instance to create an Object reference to a needed class. You would the use late-binding to work with that class instance. Alternatively, you could use Reflection to get the needed MethodInfo's/PropertyInfo's/etc. and work through them to work on the class instance, but I think that would be a lot more work than using late-binding.

Edited to add example.

Sub Test()
' assume you chose Version 2 as to reference in your project
' you can create an instance of its classes directly in your code
' with full Intellisense support

Dim myClass1V2 As New CommonRootNS.Class1

' call function Foo on this instance
Dim resV2 As Int32 = myClass1V2.foo

' to get access to Version 1, we will use Reflection to load the Dll

' Assume that the Version 1 Dll is stored in the same directory as the exceuting assembly
Dim path As String = IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly.Location)

Dim dllVersion1Assembly As Reflection.Assembly
dllVersion1Assembly = Reflection.Assembly.LoadFile(IO.Path.Combine(path, "Test DLL Version 1.dll"))

' now create an instance of the Class1 from the Version 1 Dll and store it as an Object
Dim myClass1V1 As Object = dllVersion1Assembly.CreateInstance("CommonRootNS.Class1")

' use late binding to call the 'foo' function. Requires Option Strict Off
Dim retV1 As Int32 = myClass1V1.foo
End Sub

Can two classes with the same name be in the same namespace across two different assemblies?

Yes they can. If both assemblies are referenced, you will receive an error similar to this:

error CS0433: The type 'Testing' exists in both 'MyApp.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' and 'MyApp.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

Note, you should never purposefully put yourself in this situation. However, if you for some reason find yourself in a situation where you have to handle this:

Every assembly has an "alias" in .NET and by default it is global. This is usually invisible to the day to day programmer. You can change the alias by clicking on the reference and changing the Alias in Properties. You would then reference it by having extern alias myalias at the top of your file, and the actual class referenced like this myalias::MyApp.Testing

This is perhaps needed when running 2 versions of an API or similar, although I maintain to avoid this if at all possible.

For more detail, see this answer.

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.



Related Topics



Leave a reply



Submit