Call non-static method from static method c# [dupl

2019-02-18 00:33发布

问题:

Possible Duplicate:
Call non-static method from static method c#

We can call non-static method from static method creating instance. Code:

public class Foo
{
    public void Data1()
    {
    }

    public static void Data2()
    {
        Foo foo = new Foo();
        foo.Data1();
    }
}

However, I heard that non-static method can be called from static method with the help of delegate. is it true? If yes, then how? Please guide me with sample code. Thanks.

回答1:

This is one method for calling a non-static method via a delegate. Note that it is a two-step process, since to call a non-static method, you absolutely need an instance of the class. I would also note that there is almost certainly a better way to do what you want to do, since needing to call a non-static method from a static method despite not wanting to use an object instance makes it sound like the non-static method should be static.

public class MyClass
{
    private static Action NonStaticDelegate;

    public void NonStaticMethod()
    {
        Console.WriteLine("Non-Static!");
    }

    public static void CaptureDelegate()
    {
        MyClass temp = new MyClass();
        MyClass.NonStaticDelegate = new Action(temp.NonStaticMethod);
    }

    public static void RunNonStaticMethod()
    {
        if (MyClass.NonStaticDelegate != null)
        {
            // This will run the non-static method.
            //  Note that you still needed to create an instance beforehand
            MyClass.NonStaticDelegate();
        }
    }
}


回答2:

To call a non static method you need an instance of the object - there is no way around that.



回答3:

Yes, you can call a non-static method from anywhere using a delegate, but you need the instance of the class to create the delegate, so that the instance is available when the method is called.



回答4:

You will always need an instance to call an instance method. It is possible to have a delegate that points to a method on an instance - but then you still have an instance that are being used in the call, just indirectly.



回答5:

You can do other clever things, but the reality is that along the way somewhere you have to create or access an instance of the non-static object. You could pass a delgate into the method, that refers to an instance of the method ( rather, a method on an instance of the non-static object ), but that would have had to be created outside your code.



回答6:

In the code provided you don't call non stati method in static one, but you call instance public method of another class, this is different story.

So in your case I would suggest write something like:

static void Data2(Foo foo).

Regards.



回答7:

This way like you did it can be called a non-static method from the static method. To call a non-static method from a static method, you have to create an new reference of the class in which the non-static method is in. So your Data1 method is in Foo class, you have to create a new referenc (Foo foo = new Foo()) to be allowed to go out of static method.



标签: c# oop