Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Using same class name from different assembly

Using same class name from different assembly

Scheduled Pinned Locked Moved C#
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PozzaVecia
    wrote on last edited by
    #1

    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?

    L M 2 Replies Last reply
    0
    • P PozzaVecia

      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?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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 both New and Old 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![^]

      P 1 Reply Last reply
      0
      • L Lost User

        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 both New and Old 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![^]

        P Offline
        P Offline
        PozzaVecia
        wrote on last edited by
        #3

        thanks

        1 Reply Last reply
        0
        • P PozzaVecia

          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?

          M Offline
          M Offline
          minnie mouse
          wrote on last edited by
          #4

          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();
              }
          }
          

          }

          P 1 Reply Last reply
          0
          • M minnie mouse

            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();
                }
            }
            

            }

            P Offline
            P Offline
            PozzaVecia
            wrote on last edited by
            #5

            this is perfect you get the point!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups