关于冲突类名称的问题 [英] Problem regarding confilcting Class names

查看:76
本文介绍了关于冲突类名称的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我遇到的一个问题是,我有2个类驻留在具有相同名称和相同方法签名的离散命名空间中.这是一个第三方DLL,因此我无法修改它们以使用公共基类或实现接口.我想创建该类的对象并在我的应用程序中使用.
我给你举个例子.

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



现在,我的问题是,我想基于某种逻辑来创建ABC类的对象,以便从哪个名称空间中进行选择.因此,是否有必要为两个类创建单独的对象然后在我的应用程序中使用?
我试过了:



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


但是"obj"没有得到该DoSomething()方法.
我的appl'n方法使用该ABC对象,并且我不想重载每种方法.
有没有一种方法可以定义公共对象并在我的方法中使用它而不重载它们?

谢谢


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

推荐答案

基于您对Josh_Jackson的回复,我建议您使用接口来限制这两种类型.如果它们是完全不同的功能,但是共享相同的公共协定,则可以从接口实例化:
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();


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
    {
    }
}



输入A即可打印出来:



Entering A prints out:

I''m class A.Foo



输入B即可打印出来:



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.


有了适当的约束,实现此目标的唯一方法是使用反射.我不建议这样做,因为它会增加大量的运行时开销.
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.


这篇关于关于冲突类名称的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆