0

I have a static class with number of different methods.

I have another class, and with each instance of this class, I would like it to have a method which calls one of the methods in the static class. For each instance, I want be able to specify which of the methods it will use via the constructor of this class.

Is there a simple way to do this? Should I be using delegates/interfaces?

1
  • 2
    You might get some purchase out of multicast delegates. But if you could post some code, that would help. Better design may help. Commented Jan 5, 2011 at 18:30

2 Answers 2

4

Do the methods all have the same signature? If so, a delegate would certainly be a good approach... although it wouldn't restrict the caller to passing in a method group from the static class. If that's not a problem, here's a sample:

using System;

public static class TestMethods
{
    public static void Foo(int x)
    {
        Console.WriteLine("Foo " + x);
    }

    public static void Bar(int x)
    {
        Console.WriteLine("Bar " + x);
    }
}

public class DummyClass
{
    private readonly Action<int> action;

    public DummyClass(Action<int> action)
    {
        this.action = action;
    }

    public void CallAction(int start, int end)
    {
        for (int i = start; i < end; i++)
        {
            action(i);
        }
    }
}

class Test
{
    static void Main()
    {
        DummyClass d1 = new DummyClass(TestMethods.Foo);
        DummyClass d2 = new DummyClass(TestMethods.Bar);
        d1.CallAction(2, 4);
        d2.CallAction(3, 7);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Causix - If the methods don't have the same signatures, your single class is not going to know how to call each one without using some reflection. You might wanna pass a MethodInfo rather than a delegate. Perhaps you should post some code samples so we can see clearly what you're trying to achieve.
1

Here is what you are looking for:

public delegate void MyStaticMethodInvoker(params object[] values);

public class TestStatic
{
    public static void TestMethod1(params object[] values)
    {
        Console.WriteLine("TestMethod1 invoked");
    }

    public static void TestMethod2(params object[] values)
    {
        Console.WriteLine("TestMethod2 invoked");
    }

    public static void TestMethod3(params object[] values)
    {
        Console.WriteLine("TestMethod3 invoked");
    }
}

public class TestClass
{
    private MyStaticMethodInvoker _targetMethod;

    public TestClass(MyStaticMethodInvoker targetMethod)
    {
        _targetMethod = targetMethod;
    }

    public void CallTargetedStaticMethod()
    {
        _targetMethod.Invoke(1,2,3,4);
    }
}

And then you can create instances of TestClass and in constructor define target static method:

TestClass tc1 = new TestClass(new MyStaticMethodInvoker(TestStatic.TestMethod1));
tc1.CallTargetedStaticMethod();

TestClass tc2 = new TestClass(new MyStaticMethodInvoker(TestStatic.TestMethod2));
tc2.CallTargetedStaticMethod();

TestClass tc3 = new TestClass(new MyStaticMethodInvoker(TestStatic.TestMethod3));
tc3.CallTargetedStaticMethod();

3 Comments

Why are you using new MyStaticMethodInvoker(TestStatic.TestMethod3) instead of a method group conversion of just TestStatic.TestMethod3?
To point out that delegate is used in this case. Yep, a same affect is by using TestClass tc1 = new TestClass(TestStatic.TestMethod1);
Thanks for this, I don't quite understand it all at the moment so I'll go with what Skeet said for now. It's given me food for thought though. +1

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.