C# code to handle different classes with same method names

前端 未结 9 1531
夕颜
夕颜 2020-12-10 01:56

Let\'s say you have two different C# classes A and B that while not deriving from the same base class do share some of the same names for methods.

相关标签:
9条回答
  • 2020-12-10 02:35

    There is a OOAD concept of 'Programe to an interface not to an implementation' which let's you avoid the chain of inheritance hierarchies

    1- You can create a interfcae

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

    2- And let your classes implement this interface as shown below.

    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- Once you done with the implementation than you can make your function accepting an argument of IConnection as shown below.

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

    4- And from your client code , you can pass the object of classes which implements IConnect Interface.

    0 讨论(0)
  • 2020-12-10 02:41

    This will work in C# 4:

    public void make_connection(dynamic x)
    {
      x.connect() ;
      // Do some more stuff...
      x.disconnect() ;
      return ;
    }
    
    0 讨论(0)
  • 2020-12-10 02:42

    Either you will have to use an Interface (or Base class) as shown by Zach and astander, or you will have to case the object before using:

    public void make_connection(Object x) 
    { 
      ((A)x).connect() ; 
      // Do some more stuff... 
      x.disconnect() ; 
      return ; 
    } 
    
    0 讨论(0)
  • 2020-12-10 02:42

    You could also use reflection to invoke the methods

    0 讨论(0)
  • 2020-12-10 02:43

    Try using an Interface rather.

    Have a look at interface (C# Reference) and Interfaces (C# Programming Guide)

    So something like

    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;
    }
    
    0 讨论(0)
  • 2020-12-10 02:48

    What you want is called Duck Typing.

    From Wikipedia:

    Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface.

    C# 4.0 allows this, as other have said, using the dynamic keyword

    0 讨论(0)
提交回复
热议问题