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

2019-02-18 00:40发布

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.

标签: c# oop
7条回答
够拽才男人
2楼-- · 2019-02-18 01:04

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.

查看更多
smile是对你的礼貌
3楼-- · 2019-02-18 01:07

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();
        }
    }
}
查看更多
啃猪蹄的小仙女
4楼-- · 2019-02-18 01:10

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.

查看更多
The star\"
5楼-- · 2019-02-18 01:23

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楼-- · 2019-02-18 01:24

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.

查看更多
时光不老,我们不散
7楼-- · 2019-02-18 01:26

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.

查看更多
登录 后发表回答