user1909612
user1909612

Reputation: 273

how do i assign a static method to a System.Delegate object?

My problem is that i have a class whose constructor takes a System.Delegate object as a parameter and i have no idea how to assign a method to a System.Delegate object. This is the code i have right now

class TestClass
{
    Delegate c = TestMetod;
    static void TestMetod()
    {
        MessageBox.Show("it worked !");
    }
}

but that doesn' t work because, oddly enough, System.Delegate is a non-delegate type as stated by msdna. How am i supposed to do what i need since it is impossible to "assign method group TestMetod to non-delegate type 'System.Delegate'"

Upvotes: 2

Views: 6757

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273244

The static aspect is not the core issue here. You need a (any) delegate to capture TestMethod and then you can assign that to a System.Delegate. You can use Action as such an intermediate.

class TestClass
{
    static Action a = TestMetod;
    static Delegate c = a;
    static void TestMetod()
    {
        MessageBox.Show("it worked !");
    }
}

Upvotes: 5

Related Questions