theOne
theOne

Reputation: 357

Ensure only classes that implement a certain interface can call a method? C#?

Ok, this may be a newbie question. So apologies. Lets say I have a class with a method like:

public class A 
{
    public void DoSomething(long x)
    {
    }
}

Now I want to ensure that any object or class that calls DoSomething() MUST implement a certain interface like ISomethingInterface?

Is it possible to do that in C#? Thanks

Upvotes: 3

Views: 641

Answers (4)

InBetween
InBetween

Reputation: 32740

The question is kind of strange because you are trying to implement something similar to an interface scenario but kind of upside down.

If you really want only classes implementing a certain interface to be able to call some particular method I would enforce it with extension methods:

 public static void DoSomething(this IFoo foo, long x)
 {
     ....
 }

Now you can only call DoSomething(long x) through an IFoo typed object. See extension methods for more info.

Of course an equivalent solution but not so convenient or elegant is to simply implement a static method like so:

 public static void DoSomething(IFoo foo, long x) { ... }

You are switching calling DoSomething from only IFoo objects to passing an IFoo object as an argument. Extension methods are essentially a syntactic sugar of this solution.

But this scenario really doesn't make sense unless you don't have access to IFoo's implementation (third party code) or changing the interface is a breaking change you can't afford. If you do have access or can afford the break, then just make DoSomething part of the interface. That way all IFoo objects will have a DoSomething method.

Not sure if I've understood your question correctly.

Upvotes: 2

Kanadaj
Kanadaj

Reputation: 991

You might want to use an abstract class that implements your function instead of an interface:

public abstract class MyAbstractClass
{
    //This one HAS to be defined in the inheritor class, like an interface
    public abstract void DoSomeOtherThing();

    //This is your function that can only be called by objects that inherit from this class.
    protected void DoSomething()
    {
        //use your protected method here 
    }
}

public class MyClass : MyAbstractClass
{
    public MyClass()
    {
        DoSomething();
    }

    public override void DoSomeOtherThing()
    {

    }
}

Upvotes: 0

Matthew Cox
Matthew Cox

Reputation: 13672

If you really want to enforce the scenario where a user must implement interface B if they also have A, then you can simply mark a interface to re-implement another. See MSDN - Interfaces

public interface A
{
   void a();
}

public interface B : A
{
   void b();
}

public class Foo: B
{
    public void a()
    {
    }


    public void b()
    {
    }
}

If they don't implement both methods then this will throw a compile-time error.

Upvotes: 0

Callum Linington
Callum Linington

Reputation: 14417

public class A : IA
{
       public void DoSomething(long x)
        {
           }
}

public interface IA
{
     void DoSomething(long x)
}


/*Some method somewhere*/
public void DoThisThing()
{
   IA a = new A();

   a.DoSomething(10);
}

You are sort of on the right lines, but it works the other way round. The interface tells you what you can do, it acts like a contract.

Now you can enforce your contract on other classes making sure you can only call DoSomething

public class B : IA
{
    public void DoSomething(long x)
    {
        // B's custom implementation
    }

    public void IWantSomethingElse(string s)
    { 
        // this is only class specific and cannot be called from the interface
    }
}

/* Some method somewhere */
public void DoThisThing()
{
    IA a = new B();

    a.DoSomething(2);

    // here there is no way to call IWantSomethingElse because the its not on the interface
}

Upvotes: 0

Related Questions