Faber
Faber

Reputation: 2194

Problem with Abstract class, Interface, Container and methods

I've the following scenario

I've an Interface

public interface ImyInterface
{
    void myInterfaceMethod(string param);
}

I've an Abstract Class

public abstract class myAbstractClass
{
    public myAbstractClass()
    {
       //something valid for each inherited class
    }

    public void myAbstractMethod<T>(T param)
    {
      //something with T param
    }
}

I've a class that inherits from myAbstractClass and implements ImyInterface

public class myClass : myAbstractClass, ImyInterface
{
    public myClass():base()
    {}

    public void ThisMethodWillNeverCall()
    {
       // nothing to do
    }                    
}

And, finally, I've a class where I'll create a ImyInterface object. At this point I would call myAbstractMethod, but...

public class myFinalClass
{
    public void myFinalMethod()
    {
       ImyInterface myObj = _myContainer<ImyInterface>();

       myObj.???

    }
}

Obviously there isn't this method because it isn't declared into the interface. My solution is the following

public interface ImyInterface
{
   void myInterfaceMethod(string param);
   void myFakeMethod<T>(T param);
}

public class myClass : myAbstractClass, ImyInterface
{
   public myClass():base()
   {}

   public void ThisMethodWillNeverCall()
   {
      // nothing to do
   }

   //--- a fake method
   public void myFakeMethod<T>(T param)
   {
         base.myAbstractMethod<T>(param);
   }                    
 }

Is there any other solution better than mine?

Thank you!

Upvotes: 0

Views: 214

Answers (1)

jgauffin
jgauffin

Reputation: 101150

First of all, your naming convention is a mess. Read up on the guidelines that Microsoft have made.

It's also hard to tell what you are trying to achieve based on your example.

Back to your question:

You should only access an interface to work with that interface. Don't try to make any magic stuff with classes/interfaces to get them working together. That usually means that the class shouldn't try to implement the interface.

It's better that you create a new interface which have the features that you want and let your class implement both.

Upvotes: 2

Related Questions