简体   繁体   English

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

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

I have the following code, I want to call data1() from data2() .我有以下代码,我想从data2()调用data1() data2() Is this possible in C#?这在 C# 中可能吗? If so, how?如果是这样,如何?

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

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

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 only one instance.您仍然需要创建该类的一个实例,但要确保只有一个实例。

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

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

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

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

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();
    }
}
 new Foo();
 Foo.StaticMethod();

class Foo
{
    private static Foo foo;

    public Foo()
    {
        foo = this;
    }

    private void PrintHello()
    {
        Console.WriteLine("Hello");
    }

    public static void StaticMethod()
    {
        foo.PrintHello();
    }
}

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()
{

}


}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从其他类的静态方法中调用非静态方法? - How do I call a non-static method from a static method from a different class? 如何从C#中的线程访问非静态方法 - How to access a non-static method from a thread in C# 如何在C#中的另一个类之外调用非静态方法? - How can I call a non-static method out of another class in C#? 如何用静态方法调用非静态方法(使用WebControl)? - How do I call a non-static method (that uses a WebControl) with a static method? 在 C# 中调用具有方法属性的非静态方法 - Call non-static methods with method attributes in C# 如何在Visual Studio中(从另一个非静态方法)在导入的项目中调用非静态方法 - How to call a non-static method in an imported project (from another non-static method) in Visual Studio 有没有办法从静态方法调用非静态方法? - Is there a way to call a non-static method from a static method? 如何调用非静态方法 - How to call a non-static method 在C#中编写静态和非静态方法时,如何避免出现“调用不明确……”错误? - How do I avoid 'call is ambiguous…' error when writing static and non-static methods in C#? 如何从aspx调用非静态方法 - How to call a non-static method from aspx
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM