代码之家  ›  专栏  ›  技术社区  ›  rlandster

处理具有相同方法名的不同类的C代码

c#
  •  15
  • rlandster  · 技术社区  · 14 年前

    A B connect 以及 disconnect 方法,以及其他一些方法。我想能够写一次代码,这将与两种类型的工作。

    public void make_connection(Object x)
    {
      x.connect() ;
      // Do some more stuff...
      x.disconnect() ;
      return ;
    }
    

    当然,由于对象类没有 方法。

    更新。我应该从一开始就说明这一点:我只有A和B的dll,而没有源代码。

    9 回复  |  直到 14 年前
        1
  •  26
  •   Zach Johnson    14 年前

    你可以使用一个接口来完成你想做的事情。

    interface IConnectable
    {
        void Connect();
    
        void Disconnect();
    }
    

    A B 应该执行 IConnectable 可连接 Object 作为方法的参数类型,应该全部设置。

    public void MakeConnection(IConnectable connectable)
    {
        connectable.Connect();
    
        // Do some more stuff...
    
        connectable.Disconnect();
    }
    

    编辑: 因为您没有源代码,所以有两个选项:

    1. dynamic 关键字(如果您使用的是.NET 4.0)
    2. 使用Steve使用casting和 if else 声明
    3. 为创建包装类 并让他们实现接口(或者为他们使用公共抽象基类)

    例如:

    class AWrapper : IConnectable
    {
        private A obj;
    
        public AWrapper(A obj)
        {
            this.obj = obj;
        }
    
        public void Connect()
        {
            this.obj.Connect();
        }
    
        public void Disconnect()
        {
            this.obj.Disconnect();
        }
    
        // other methods as necessary
    }
    

    ( BWrapper 会很相似,只是用 一个 )

    MakeConnection . 你想怎么做取决于你。根据你的情况,一种方法可能比其他方法容易。

        2
  •  11
  •   Max    14 年前

    public void make_connection(dynamic x)
    {
      x.connect() ;
      // Do some more stuff...
      x.disconnect() ;
      return ;
    }
    
        3
  •  2
  •   Adriaan Stander    14 年前

    试着使用一个接口。

    看一看 interface (C# Reference) Interfaces (C# Programming Guide)

    所以有点像

    public interface IConnections
    {
        void connect();
        void disconnect();
    }
    
    public class A : IConnections
    {
        public void connect()
        {
            //do something
        }
    
        public void disconnect()
        {
            //do something
        }
    }
    
    public class B : IConnections
    {
        public void connect()
        {
            //do something
        }
    
        public void disconnect()
        {
            //do something
        }
    }
    
    public void make_connection(IConnections x)
    {
        x.connect();
        // Do some more stuff... 
        x.disconnect();
        return;
    }
    
        4
  •  2
  •   TalentTuner    14 年前

    有一个OOAD的概念 '编程到接口而不是实现'

    1-您可以创建一个intercae

    interface IConnection
    {
        void Connect();
        void Disconnect();
    }
    

    让你的类实现这个接口,如下所示。

    class A : IConnection
    {
        #region IConnection Members
    
        public void Connect()
        {
           // your connect method implementation goes here.
        }
    
        public void Disconnect()
        {
            // your disconnect method implementation goes here.
        }
    
        #endregion
    }
    
    
    class B : IConnection
    {
        #region IConnection Members
    
        public void Connect()
        {
             // your connect method implementation goes here.
        }
    
        public void Disconnect()
        {
            // your disconnect method implementation goes here.
        }
    
        #endregion
    }
    

    3-完成实现后,就可以使函数接受IConnection的参数,如下所示。

    public void makeConnection(IConnection con)
        {
            con.Connect();
            con.Disconnect();
        }
    

        5
  •  1
  •   fhj    14 年前

    如果接口解决方案不可行(例如您没有源代码),另一个效率较低的解决方案是使用反射。

        6
  •  1
  •   Steve    14 年前

    正如其他人所说,重新分解以使用接口或使用动态方法可能是最优雅的方法。

    如果这不可能,可以将对象强制转换为类型。我建议你用 as 然后检查强制转换是否有效,如果有人使用未能强制转换的类型调用此类型,则未经检查的强制转换将是危险的。

    E、 g.如果类型 A B DoSomething() 这样就行了。。。

        public static void CallDoSomething(object o)
        {
            A aObject = o as A;
    
            if (aObject != null)
            {
                aObject.DoSomething();
                return;
            }
    
            B bObject = o as B;
    
            if (bObject != null)
            {
                bObject.DoSomething();
                return;
            }
        }
    

    但是

        7
  •  0
  •   Bhaskar    14 年前

    必须使用Zach和astander所示的接口(或基类),或者必须在使用以下对象之前对其进行大小写:

    public void make_connection(Object x) 
    { 
      ((A)x).connect() ; 
      // Do some more stuff... 
      x.disconnect() ; 
      return ; 
    } 
    
        8
  •  0
  •   Fredrik Leijon    14 年前

    您还可以使用反射来调用方法

        9
  •  0
  •   Carlos Muñoz Boom    14 年前

    Duck Typing .

    鸭子打字

    正如其他人所说,C#4.0允许使用 关键字