如何调用 Creator 方法

发布于 2024-12-05 21:10:53 字数 706 浏览 2 评论 0原文

如果我们有一个类

public class A
{
    public void resetValues()
    {
        //DoSomething
    }

    B b = new B();
}
public class B
{
    public B()
    {

    }

    void doSomething()
    {
        //Is there a way here to call A(the Creator) method resetValues();
        //Like Creator.resetValues();
    }
}

,那么有没有一种方法可以调用 Creator 方法,就像这个示例中的类 A 方法的 . 当我使用一个表单来显示另一个表单时,这是非常有必要的: 来自 FormA

FormB frmB = new FormB();
frmB.Show();
this.hide();

我应该 onFormClose FormB 事件再次显示 FormA

编辑 首先我认为将 Ref 解析为对象,但后来将引用存储为字段我发现这是不可能的!

首先我想也许使用反射我们可以识别并调用 Creator 方法,但我认为我不匹配一些OOP 设计模式

If we have a Class

public class A
{
    public void resetValues()
    {
        //DoSomething
    }

    B b = new B();
}
public class B
{
    public B()
    {

    }

    void doSomething()
    {
        //Is there a way here to call A(the Creator) method resetValues();
        //Like Creator.resetValues();
    }
}

So is there a way to call the Creator methods like in this Example Class A method's .
This is is very needful when i use a Form to show Another Form :
from FormA

FormB frmB = new FormB();
frmB.Show();
this.hide();

Than i should onFormClose Event of FormB to Show again the FormA

EDIT
First i thought parsing A by Ref as Object ,but Storing a Reference as a Field later i founded out that's impossible !

First i thought maybe using reflection we can Identify and Call Creator method's but i think i mismatched some of OOP Design Pattern's

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只有影子陪我不离不弃 2024-12-12 21:10:53

唯一的方法是在调用构造函数时传入“creator”:

 B b = new B(this);

当然,这意味着 B 需要有一个具有适当参数类型的构造函数,该构造函数应该与它需要执行的任何操作相匹配它的创造者。

编辑:正如评论中所指出的,它实际上并不需要在构造时完成 - 它可能是一个属性:

 B b = new B();
 b.Creator = this;

但它们基本上是相同的东西。

老实说,我认为这通常是一个坏主意。它在两个类之间引入了相当紧密的耦合。对于 B 来说,发布一个创建者可以订阅的事件可能会更好,以便处理 B 上适当的状态更改等。

The only way is to pass in the "creator" when calling the constructor:

 B b = new B(this);

Of course, that means that B needs to have a constructor with the appropriate parameter type, which should match whatever it needs to do with its creator.

EDIT: As noted in comments, it doesn't really need to be done at construction - it could be a property:

 B b = new B();
 b.Creator = this;

They amount to basically the same thing though.

I would suggest this is usually a bad idea, to be honest. It's introducing a reasonably tight coupling between the two classes. It might be better for B to publish an event which the creator can subscribe to, in order to handle appropriate state changes etc on B.

还给你自由 2024-12-12 21:10:53

@Jon Skeet答案,B类最好在发生事件时发出事件,而A类则更可取消耗该事件。

这样,类 B 就不再依赖于类 A。您可以将类 B 与类 A 以外的类重用,而无需修改类 B 的内部结构。从维护的角度来看,这是更可取的。

您可以按如下方式设置:

public class A
{
    B b = new B();

    public A()
    {
       b.SomethingHappened += SomethingHappenedHandler; 
    }
    public void resetValues()
    {
        //DoSomething
    }
    public void SomethingHappenedHandler(object sender, EventArgs args)
    {
        resetValues();
    }

}

public class B
{
    public event EventHandler SomethingHappened;
    public B()
    {

    }

    void doSomething()
    {
        var ev = SomethingHappened;
        if(ev != null)
        {
            ev(this, EventArgs.Empty);
        }
    }
}

Following on from @Jon Skeet's answer, it would be preferable for class B to emit an event when something happens and for class A to consume that event.

In this way, class B has no dependency on class A. You could reuse class B with classes other than class A without modifying the internals of class B. From a maintenance POV, this is far more preferable.

You could set it up as follows:

public class A
{
    B b = new B();

    public A()
    {
       b.SomethingHappened += SomethingHappenedHandler; 
    }
    public void resetValues()
    {
        //DoSomething
    }
    public void SomethingHappenedHandler(object sender, EventArgs args)
    {
        resetValues();
    }

}

and

public class B
{
    public event EventHandler SomethingHappened;
    public B()
    {

    }

    void doSomething()
    {
        var ev = SomethingHappened;
        if(ev != null)
        {
            ev(this, EventArgs.Empty);
        }
    }
}
清浅ˋ旧时光 2024-12-12 21:10:53

您可以简单地使用 Action 代替 Func,返回 bool 允许传回指示操作是否成功执行的状态。

class B
{ 
  private Func<bool> executeExternal;

  public B(Func<bool> executeExternal) 
  {
       this.executeExternal= executeExternal;

       // here is resetValues() will be executed in case of B class
       bool isDoneSuccessfully = this.executeExternal();
  } 
}

public class A 
{     
  public void resetValues()     
  {         
     //DoSomething     
  }      

  // basically we injecting a delegate
  B b = new B(() => { this.resetValues(); return true; } ); 

} 

You can use simply Action instead Func<bool>, returning bool allows passing back a state which indicating whether operation was executed successfully.

class B
{ 
  private Func<bool> executeExternal;

  public B(Func<bool> executeExternal) 
  {
       this.executeExternal= executeExternal;

       // here is resetValues() will be executed in case of B class
       bool isDoneSuccessfully = this.executeExternal();
  } 
}

public class A 
{     
  public void resetValues()     
  {         
     //DoSomething     
  }      

  // basically we injecting a delegate
  B b = new B(() => { this.resetValues(); return true; } ); 

} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文