0

I have two classes Say A and B which has method set().

   public Class A : I<string>
   {
      void Set(string str)
      {
         //do something
      }
   }

   public Class B : I<int>
   {
      void Set(int str)
      {
         //do something
      }
   }

And an interface as follows...

interface I<T>
{
    void Set(T param);
}

I would like to access this method without instantiating the classes, through interface (Is it possible or is there any other way like dependency injection?).

From another Class

Class D
{
    I.Set(<T> str); //something like this
}

So based on data type I need to redirect the call from either interface or some where, so that if tomorrow I added a class say C which implements same interface, I should not end up with changing code in D.

Thanks in Advance...

7
  • 3
    Without instances of A or B (or some future C), on what are you actually expecting to set values? Commented Jul 29, 2015 at 11:30
  • 1
    Without actual objects instances in memory, I'm not sure what this question is trying to achieve. You cannot call an interface like a class e.g. I.Set() since an interface is nothing more than a contract, it's not an actual object in memory that is instantiated. Commented Jul 29, 2015 at 11:33
  • @Mathew Abbott I have the parameter type of set method to distinguish this... My first question is, Is this possible? Or else any other solution for this? Commented Jul 29, 2015 at 11:35
  • @LijoVarghese You misunderstand what Matthew aims at: You can not call anything on an interface, because always an object is needed to perform something. Commented Jul 29, 2015 at 11:51
  • Change set to accept object and cast inside imoemented methods to T Commented Jul 29, 2015 at 12:09

2 Answers 2

4

An interface is sort of like a template of methods an implementing class provides. You can not "do anything" with an interface. You always need an instance of a class implementing the interface.

So what you want does not work. However, a simple extension method will help you here:

public static class MyExtensionMethods
{
    public static void SetValue<T>(this I<T> intf, T value)
    {
        intf.Set(value);
    }
}

Using this, you can write:

A a = new A();
B b = new B();

b.SetValue("Hello");
a.SetValue(1);

And it will work for any other classes that implement I<T> without having to change the extension method:

public class D : I<double>
{
    public void Set(double d) { ... }
}

D d = new D();
d.SetValue(42.0);
Sign up to request clarification or add additional context in comments.

Comments

0

You need to pass in something, so at the moment, my best guess would be

class D
{
  public void Set<T>(object target, T value) 
  {
    var instance = target as I<T>;
    if (instance != null)
    {
      instance.Set(value);
    }
  }
}

Called like:

var theD = new D();
var theA = new A();
var theB = new B();
theD.Set<string>(theA, "hello");
theD.Set<int>(theB, 1);

1 Comment

While that works it has two flaws in my opinion: a) it doesn't warn you about errors at compile time and b) it simply does nothing if you pass in invalid parameters. It just doesn't set any values. This may lead to bugs that are really a PITA to debug...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.