如何从 C# 中的静态方法调用非静态方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1360183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:37:03  来源:igfitidea点击:

How do I call a non-static method from a static method in C#?

c#

提问by Yayan

I have the following code, I want to call data1()from data2(). Is this possible in C#? If so, how?

我有以下代码,我想data1()data2(). 这在 C# 中可能吗?如果是这样,如何?

private void data1()
{
}
private static void data2()
{
   data1(); //generates error
}

回答by tvanfosson

You'll need to create an instance of the class and invoke the method on it.

您需要创建该类的一个实例并在其上调用该方法。

public class Foo
{
    public void Data1()
    {
    }

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

回答by Brandon

You can't call a non-static method without first creating an instance of its parent class.

您不能在不首先创建其父类的实例的情况下调用非静态方法。

So from the static method, you would have to instantiate a new object...

因此,从静态方法中,您必须实例化一个新对象...

Vehicle myCar = new Vehicle();

... and then call the non-static method.

...然后调用非静态方法。

myCar.Drive();

回答by Jim W

You have to create an instance of that class within the static method and then call it.

您必须在静态方法中创建该类的实例,然后调用它。

For example like this:

例如像这样:

public class MyClass
{
   private void data1()
   {
   }
   private static void data2()
   {
     MyClass c = new MyClass();
     c.data1();
   }
}

回答by Kepboy

Perhaps what you are looking for is the Singleton pattern?

也许您正在寻找的是单例模式?

public class Singleton
{
    private Singleton() {}

    public void DoWork()
    { 
        // do something
    }

    // You can call this static method which calls the singleton instance method.
    public static void DoSomeWork()
    { 
        Instance.DoWork();
    }

    public static Singleton Instance
    {
        get { return instance; } 
    }

    private static Singleton instance = new Singleton();
}

You still have to create an instance of the class but you ensure there is onlyone instance.

您仍然需要创建该类的一个实例,但要确保只有一个实例。

回答by Leo

You just need to provide object reference . Please provide your class name in the position.

您只需要提供对象引用。请在职位中提供您的班级名称。

private static void data2()
{
    <classname> c1 = new <classname>();
    c1. data1();
}

回答by Theophilus

Assuming that both data1()and data2()are in the same class, then another alternative is to make data1()static.

假设data1()data2()都在同一个类中,那么另一种选择是使data1()静态。

private static void data1()
{
}
private static void data2()
{
   data1();
}

回答by Mou

Apologized to post answer for very old thread but i believe my answer may help other.

很抱歉为非常旧的帖子发布答案,但我相信我的回答可能会帮助其他人。

With the help of delegate the same thing can be achieved.

在委托的帮助下,可以实现同样的事情。

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();
        }
    }
}

回答by Angad

Static method never allows a non-static method call directly.

静态方法从不允许直接调用非静态方法。

Reason: Static method belongs to its class only, and to nay object or any instance.

原因:静态方法只属于它的类,不属于任何对象或任何实例。

So, whenever you try to access any non-static method from static method inside the same class: you will receive:

因此,每当您尝试从同一类中的静态方法访问任何非静态方法时:您将收到:

"An object reference is required for the non-static field, method or property".

“非静态字段、方法或属性需要对象引用”。

Solution: Just declare a reference like:

解决方案:只需声明一个引用,如:

public class <classname>
{
static method()
{
   new <classname>.non-static();
}

non-static method()
{

}


}

回答by ???

You can use call method by like this : Foo.Data2()

您可以像这样使用调用方法:Foo.Data2()

public class Foo
{
    private static Foo _Instance;

    private Foo()
    {
    }

    public static Foo GetInstance()
    {
        if (_Instance == null)
            _Instance = new Foo();
        return _Instance;
    }

    protected void Data1()
    {
    }

    public static void Data2()
    {
        GetInstance().Data1();
    }
}