C# 如何在不实例化的情况下引用另一个类的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1613964/
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 19:24:03  来源:igfitidea点击:

How can I reference a method from another class without instantiating it?

c#winformsformsclass

提问by Sergio Tapia

I don't want to create an object because it won't affect my visible form. How can I call a method so it does its thing in the visible side of things.

我不想创建一个对象,因为它不会影响我的可见形式。我怎样才能调用一个方法,让它在事物的可见方面做它的事情。

public class foo
{
   public void SetString(string foo)
   {
       label1.Text = foo;
   }
}

Inside another class:

在另一个类中:

foo X = new foo();
X.SetString("testlololol");

This will set the label, but VIRTUALLY, I won't be able to see it on my form.

这将设置标签,但实际上,我将无法在我的表单上看到它。

How can I do the same thing, but on my VISIBLE side of things?

我怎么能做同样的事情,但在我可见的一面?

采纳答案by RaYell

When you create your visible form store a references to it in some static property. Then other classes can use that property to run public methods of that class.

当您创建可见表单时,会将对其的引用存储在某个静态属性中。然后其他类可以使用该属性来运行该类的公共方法。

// the original form
class MyForm()
{
     // form public method
     public void MyMethod() { ... }
}

// class storing the reference to a form
class MyOtherClass
{     
    public static Form MyForm;

    public void ShowForm()
    {
        MyForm = new MyForm();
        MyForm.Show();
    }
}

// invoke form public method in this class
class YetAnotherClass
{
    public void SomeMethod ()
    {
        MyOtherClass.MyForm.MyMethod();
    }
}

回答by Jon Skeet

You need to somehow get the instance which isvisible. Work out some information path from things that already know about your form (or whatever it is) to your other code. Consider what would happen if there were two visible forms - which one would you want? That should suggest a way forward. If you know for a fact that there'll only ever be one visible instance, you coulduse a singleton - but I'd strongly suggest that you don't.

你需要以某种方式获得该实例可见的。制定一些信息路径,从已经了解您的表单(或其他任何内容)到您的其他代码。考虑一下如果有两种可见的形式会发生什么——你想要哪一种?这应该表明前进的方向。如果您知道永远只有一个可见实例的事实,您可以使用单例 - 但我强烈建议您不要。

Bear in mind that you may not need to know of it by its full type name - if this is crossing layers, you may want to work out some interface including the action in some abstract way.

请记住,您可能不需要通过其完整的类型名称来了解它 - 如果这是跨层,您可能需要以某种抽象方式制定一些接口,包括操作。

回答by msergeant

Your second class needs to have a reference to the instance of the class that IS visible.

您的第二个类需要引用可见的类的实例。

public class OtherClass{
  foo myFoo;

  public OtherClass( foo visibleFoo )
  {
     myFoo = visibleFoo;
  }

  public void method()
  {
     myFoo.SetString("testlolol");
  }

}

}

回答by CodingWithSpike

I would usually either pass a reference of my form ('foo' in this case) to the other class. Or I would store off a copy of 'foo' to some static location. If you know that there will only ever be 1 instance of 'foo' you could do something like:

我通常要么将我的表单引用(在本例中为'foo')传递给另一个类。或者我会将“foo”的副本存储到某个静态位置。如果您知道只有 1 个 'foo' 实例,您可以执行以下操作:

public class foo
{
   public static foo Current { get; private set; }

   public foo()
   {
       foo.Current = this;
   }

   public void SetString(string foo)
   {
       label1.Text = foo;
   }
}

...and...

...和...

foo.Current.SetString("testlololol");

Though thats a bit hacky IMO, and doesnt support multiple instances of 'foo'.

虽然这有点 hacky IMO,并且不支持 'foo' 的多个实例。