简体   繁体   中英

Action<T> equivalent for properties

I have a class that instantiates two classes which implement interfaces. I want one class to notify another class that something is OK. I could do it with an Action and then use private variables in the class but wondered if there was a direct way of doing it with properties so that when a property's value changes it updates a property on another class.

For example:

public class MyClass
{
  public ILogger Logger {get;set;}
  public ILogic Logic {get;set;}

  private Form MyWinform;

  public void Setup()
  {
    MyWinform = new MyWinform();
    MyWinform.StartBatch += Logger.CreateFile; //Create file when user presses start

    //How can I set a property on ILogic to be AllOk once ILogger says so??

    //I could use an Action so that once all is ok I call IDecidedAlOK in ILogger which
    //can then assign a private bool variable inside the class

    Logic.ItsOKMethodSoSetVariableToTrue = Logger.IDecidedAllOKMethod;

  }

  public void DataIn(string Value)
  {
     Logic.DataIn(Value);
  }

  public void CreateInstances()
  {
     Logger = new FileLogger();
     Logic = new MyLogic();
  }

}

public class MyLogic : ILogic
{
   public void DataIn(string Value)
  {
     //I want to check that all is ok before I do anything

     //if (!AllOK) 
     //return;

     //Do stuff
  }
}

实现INotifyPropertyChanged接口并订阅PropertyChanged事件

I feel like it might be a bit more conventional to have your ILogger interface expose something like a "FileCreated" or "Ready" event, and allow your application to handle that event in order to update the ILogic object (or do whatever else is necessary).

EDIT: my apologies, after re-reading the question, I think I misunderstood what you were asking for.

There isn't any "natural" object that does exactly what you're asking, but you could create an anonymous delegate (or lambda expression) for this purpose:

Action<bool> loggerResult = (value) => Logic.ItsOKMethodSoSetVariableToTrue = value;

一个属性内部由两个私有方法组成,一个是get_XXX和一个set_XXX,所以除非你想获取那些方法的MethodInfo并调用它们(它们再次是方法),否则你别无选择,只能实现方法调用方法。

Subscribing to event ( INotifyPropertyChanged or some custom one) is OK, so is the method to pass a lambda-setter, but in some cases it might be more convinient to use a shared context object (much like the shared memory concept):

class ConversationContext
{
   public bool EverythingIsOK { get; set;}
}

This object is passed to all interested objects ( ILogic and ILogger ) and they operate directly on it, instead of some internal properties. If change notifications are required, Implement INotifyPropertyChanged on it.

One positive aspect of this approach is that you won't get tangled in repeatedly firing events triggering other events and so on. A single object will hold the current state and no recurrent updates are needed.

Again, this is just one of many options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM