Click here to Skip to main content
15,903,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I am facing a problem where I have 2 classes residing in discrete namespaces with the same name and same method signatures. This is a 3rd party DLL so I cannot modify them to use a common base class, or implement an interface. I want to create object of that class and use in my application.
I'll give you an example.

namespace A
{
   public class ABC
   {
      public void DoSomething()
      {
         MessageBox.Show("ABC of A");
      }
   }
}

namespace B
{
   public class ABC
   {
      public void DoSomething()
      {
         MessageBox.Show("ABC of B");
      }
   }
}


Now, my problem is, i want to create an object of ABC class based on some logic to choose from which namespace. So, is it necessary that i create separate objects for both classes and then use in my application?
I tried this :
object obj;
if(somecondition)
{
   A.ABC objABC = new A.ABC();
   obj = (A.ABC)objABC;
}
else
{
   B.ABC objABC = new B.ABC();
   obj = (B.ABC)objABC;
}

but "obj" doesn't get that DoSomething() method.
My appl'n methods use that ABC object and i don't want to overload each and every method.
Is there a some way by which i can define common object and use it in my methods without overloading them?

Thanks
Posted
Updated 9-May-10 23:11pm
v2

Based on your reply to Josh_Jackson, I'd suggest that you use an interface to constrain both types. If they are completely different functionality, but share the same public contract then you can instantiate from the interface:
public interface IFoo
{
  void DoSomething();
}

public class Foo : IFoo
{
  public void DoSomething()
  {
    Console.WriteLine("Foo");
  }
}

public class Bar : IFoo
{
  public void DoSomething()
  {
    Console.WriteLine("Foo");
  }
}
Then, you can use this by referencing the interface as the type (and instantiating it with the concrete implementation):
IFoo instance = null;
if (somecondition)
{
  instance = new Foo();
}
else
{
  instance = new Bar();
}
instance.DoSomething();
 
Share this answer
 
Comments
nagendrathecoder 10-May-10 5:06am    
Actually, these 2 namespaces resides in two different versions of dlls of a third party product.
I want my appl'n to work on both versions of that product.
But for that to achieve, i have to create 2 different objects of same class in two different namespaces and also have to overload my all methods.
I wanted some way to create object so as i don't need to overload my all methods.

I tried your method but still with that too, i have to create 2 different objects as i can't modify thrid party dlls.
Pete O'Hanlon 10-May-10 5:09am    
You could have told us this information in your original post. Please, we can't read your mind, and this is highly pertinent information. When you pose a question, you must give all the detail needed to answer otherwise people will not be able to answer properly.
nagendrathecoder 10-May-10 5:16am    
I understand that and i apologize for that.
Is there any way to achieve what i need?
It'll save loads of work for me.
C#
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter the letter A or B");
            string s = Console.ReadLine();
            A.Base baseClass = new A.Base();
            ;
            if (s == "A")
            {
                A.Foo f = new A.Foo();
                baseClass = f;
            }
            if (s == "B")
            {
                B.Bar b = new B.Bar();
                baseClass = b;
            }
            baseClass.Write();
            Console.ReadLine();
        }
    }
}
namespace A
{
    public class Base
    {
        public virtual void Write()
        {
            Console.WriteLine("I'm class {0}", this.GetType().ToString());
        }
    }
    public class Foo : A.Base
    {
    }
}
namespace B
{
    public class Bar : A.Base
    {
    }
}


Entering A prints out:
I'm class A.Foo


Entering B prints out:
I'm class B.Bar


Basic inheritance states that methods not need to be overridden as long as the base class has an implementation.
 
Share this answer
 
v2
Comments
nagendrathecoder 10-May-10 3:06am    
Thanks for answering, i got your point.
But in my case, B.Bar doesn't inherit A.Base.
Both A and B have totally different implementations and don't reference each other.
But both provides same functionality, same methods, same properties.
So, i want to use functionality of both namespaces without creating different objects for their classes.
Joshua Tully 10-May-10 4:02am    
Is there any reason with A and B cannot inherit from a single base class? To me it seems if they have the same methods, members, and other functionality they should inherit from a base class. Anyways, since I'm not quite sure the specific reason why inheritance can't be used you might want to look into reflection for greater control of metadata (http://msdn.microsoft.com/en-us/library/f7ykdhsy(v=VS.80).aspx).
nagendrathecoder 10-May-10 5:07am    
Actually, these 2 namespaces resides in two different versions of dlls of a third party product.
I want my appl'n to work on both versions of that product. But for that to achieve, i have to create 2 different objects of same class in two different namespaces and also have to overload my all methods.
I wanted some way to create object so as i don't need to overload my all methods.
With the constraints you have in place, the only way to achieve this would be to use reflection. I wouldn't advise this as it adds a lot of runtime overhead.
 
Share this answer
 
From what you've said so far, it looks like your best approach is just to write the code you need to write. I don't recall if you mentioned whether or not the 3rd party namespaces are the same (I don't see why they wouldn't be).

It sounds to me like your high-minded desires are holding the project up. If it were me, I would link to DLLs that aren't in the GAC so that they get shipped with the product, and at that point, you don't have to do anything weird in YOUR code because the DLLs it is compatible with are shipped with the product.
 
Share this answer
 
Thanks to all for help, i tried Reflection too but as Pete has mentioned, it is adding lots of overheads.
So, i have decided to overload my methods. :doh:
But it would be appreciable if someone comes with a concrete solution on this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900