我有一個類實例化兩個實現接口的類。我想讓一個班級通知另一個班級某些事情是可以的。我可以用Action來完成,然後在類中使用私有變量,但是想知道是否有直接的方法來處理屬性,以便在屬性值更改時更新另一個類的屬性。動作<T>等價於屬性
例如:
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
}
}
您是否擁有ILogger類,我的意思是,您可以對其進行更改嗎? – Stormenet 2012-03-27 13:57:14
'ILogger'是否可以暴露'ILogic'可以訂閱的'AllOK'事件? – mellamokb 2012-03-27 13:57:22
@Stormenet是的,我可以 – Jon 2012-03-27 14:02:43