Using same class name from different assembly
-
Suppose I have a solution containing a console project ("TestConsole") and two library projects ("New" and "Old"). New and Old contains the same class called "MyClass", each one with the same name. I add both as reference to "TestConsole" In "TestConsole" can I switch the use of MyClass choosing from which assembly the class come (New or Old), in the code?
-
Suppose I have a solution containing a console project ("TestConsole") and two library projects ("New" and "Old"). New and Old contains the same class called "MyClass", each one with the same name. I add both as reference to "TestConsole" In "TestConsole" can I switch the use of MyClass choosing from which assembly the class come (New or Old), in the code?
TheGermoz wrote:
In "TestConsole" can I switch the use of MyClass choosing from which assembly the class come (New or Old), in the code?
Not so easily. You could load a type, based on it's name - but the name of the type has to be unique within the namespaces loaded. If you'd program against an
Interface
, you could use "any" object that implements that interface. Another sweet alternative would be to use a base-class, and inherit bothNew
andOld
from the same base.Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]
-
TheGermoz wrote:
In "TestConsole" can I switch the use of MyClass choosing from which assembly the class come (New or Old), in the code?
Not so easily. You could load a type, based on it's name - but the name of the type has to be unique within the namespaces loaded. If you'd program against an
Interface
, you could use "any" object that implements that interface. Another sweet alternative would be to use a base-class, and inherit bothNew
andOld
from the same base.Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]
thanks
-
Suppose I have a solution containing a console project ("TestConsole") and two library projects ("New" and "Old"). New and Old contains the same class called "MyClass", each one with the same name. I add both as reference to "TestConsole" In "TestConsole" can I switch the use of MyClass choosing from which assembly the class come (New or Old), in the code?
Do you mean like this, same class name different namespace ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{ClassLibrary1.Class1 myClass = new ClassLibrary1.Class1(); ClassLibrary2.Class1 myClass2 = new ClassLibrary2.Class1(); } }
}
-
Do you mean like this, same class name different namespace ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{ClassLibrary1.Class1 myClass = new ClassLibrary1.Class1(); ClassLibrary2.Class1 myClass2 = new ClassLibrary2.Class1(); } }
}
this is perfect you get the point!