Make sure only classes that implement a specific interface can call the method? FROM#?

Ok, this might be a newbie question. So sorry. Let's say I have a class with a method like:

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

      

Now I want to make sure that any object or class that calls DoSomething () should implement a specific interface like ISomethingInterface?

Is it possible to do this in C #? Thanks to

+3


source to share


4 answers


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

If you really want only classes that implement a specific interface to be able to call any specific method, I would apply it using extension methods:

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

      

Now you can only call DoSomething(long x)

through a IFoo

typed object. See extension methods for details .

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



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

      

You DoSomething

only call from objects IFoo

to pass the object IFoo

as an argument. Extension methods are essentially syntactic sugar for this solution.

But this scenario really doesn't make sense unless you have access to the implementation IFoo

(third-party code) or changing the interface is a violation you cannot afford. If you have access or can afford a break, just do the DoSomething

interface part. Thus, all objects IFoo

will have a method DoSomething

.

Not sure if I understood your question correctly.

+2


source


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 seem to be on the right lines, but it works the other way around. The interface tells you what you can do, it acts like a contract.



You can now apply your contract to other classes, making sure you can only call DoSomething

public class B : IA
{
    public void DoSomething(long x)
    {
        // B 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
}

      

0


source


If you really want to apply the scenario where the user has to implement interface B if they also have A, then you can simply mark the interface to re-implement the other. 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, it will cause a compile-time error.

0


source


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()
    {

    }
}

      

0


source







All Articles