C#イベントと依頼(C#学習ノート03)

16423 ワード

1. C# C C++ 。 , 。
2. , 。 ,  System.Delegate  。
:( )
delegate <return type> <delegate-name> 

 
委任使用例:
using System;

public delegate void Mydelegate(string str);                //      

namespace Delegate
{
    class TextMethods

    {
        public static void Method1(string str)
        {
            Console.WriteLine("    1,{0}",str);
        }

        public static void Method2(string str)

        {
            Console.WriteLine("    2,{0}",str);
        }

        public static void Method3(string str)
        {
            Console.WriteLine("    3,{0}", str);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Mydelegate d1, d2, d3;                               //      

            d1 = TextMethods.Method1;

            d2 = TextMethods.Method2;

            d3 = TextMethods.Method3;

            d1("1");                                             //      

            d2("2");

            d3("3");

            Console.WriteLine("");

            Mydelegate d4;

            d4 = TextMethods.Method1;

            d4 += TextMethods.Method2;                          //    

            d4 += TextMethods.Method3;

            d4("4");

            Console.WriteLine("");

            d4 -= TextMethods.Method3;                          //    

            d4("5");

            Console.WriteLine("");
        }
    }
}

 
≪イベント|Events|ldap≫
イベントは、アプリケーションが実行中に注目するアクションですが、これらのアクションが発生すると、プログラムが応答する必要があります.イベントの概念は比較的広く,すべてのプログラムが応答処理を必要とする動作をイベントと呼ぶことができる.マウスクリック、キーボード入力、タイマーメッセージなど...
イベントは依頼に基づいて、依頼に対して発行/購読メカニズムを提供した.NETアーキテクチャの内外にイベントが表示されます.Windowsアプリケーションでは、ButtonクラスがClickイベントを提供します.このようなイベントは依頼であり,Clickイベント呼び出しをトリガするプロセッサは定義する必要があり,そのパラメータは依頼タイプによって定義される.
イベントメカニズムはメッセージに基づいており、特定のアクションが発生すると対応するメッセージが生成され、そのイベントに注目するアプリケーションがイベント発生のメッセージを受信すると、処理プロセスの指定が開始されます.
例:
参考自:[w 3 cSchool]https://www.w3cschool.cn/wkcsharp/yvoj1nvx.html"%3Ehttps://www.w3cschool.cn/wkcsharp/yvoj1nvx.html%3C/a%3E
この例は、給湯ボイラシステムのトラブルシューティングに使用される簡単なアプリケーションです.メンテナンスエンジニアがボイラを検査すると、ボイラの温度、圧力、エンジニアが書いた注釈が自動的にログファイルに記録されます.例では、プライマリ関数では、イベント・パブリッシャー・クラス(DelegateBoilerEvent)が作成され、ドキュメントに書き込まれた(サブスクライバ)関数とコンソール出力関数がイベントの例に追加されていることがわかります.フリップフロップクラスのロギングプロセス関数(LogProcess())を実行すると、イベントに追加されたすべての関数インスタンスが呼び出されます.
 
using System;
using System.IO;

namespace BoilerEventAppl
{
    // boiler  
    class Boiler
    {
        private int temp;                             //    
        private int pressure;                         //    
        public Boiler(int t, int p)                   //    
        {
            temp = t;
            pressure = p;
        }

        public int getTemp()
        {
            return temp;
        }

        public int getPressure()
        {
            return pressure;
        }
    }

    //      
    class DelegateBoilerEvent
    {
        public delegate void BoilerLogHandler(string status);            //    

        //           
        public event BoilerLogHandler BoilerEventLog;

        public void LogProcess()                                         //    
        {
            string remarks = "O. K";
            Boiler b = new Boiler(100, 12);
            int t = b.getTemp();
            int p = b.getPressure();
            if (t > 150 || t < 80 || p < 12 || p > 15)                  
            {
                remarks = "Need Maintenance";
            }
            OnBoilerEventLog("Logging Info:
"); OnBoilerEventLog("Temparature " + t + "
Pressure:
" + p); OnBoilerEventLog("
Message:
" + remarks); } protected void OnBoilerEventLog(string message) // , , BoilerEventLog { if (BoilerEventLog != null) { BoilerEventLog(message); } } } // class BoilerInfoLogger { FileStream fs; StreamWriter sw; public BoilerInfoLogger(string filename) { fs = new FileStream(filename, FileMode.Append, FileAccess.Write); sw = new StreamWriter(fs); } public void Logger(string info) // { sw.WriteLine(info); } public void Close() { sw.Close(); fs.Close(); } } // public class RecordBoilerInfo { static void Logger(string info) // { Console.WriteLine(info); }//end of Logger static void Main(string[] args) { BoilerInfoLogger filelog = new BoilerInfoLogger("‪boiler.txt"); // DelegateBoilerEvent boilerEvent = new DelegateBoilerEvent(); // DelegateBoilerEvent , boilerEvent.BoilerEventLog += new DelegateBoilerEvent.BoilerLogHandler(Logger); //boilerEvent.BoilerEventLog , Logger boilerEvent.BoilerEventLog += new DelegateBoilerEvent.BoilerLogHandler(filelog.Logger); //filelog BoilerInfoLogger , Logger boilerEvent.LogProcess(); // LogProccess filelog.Close(); }//end of main }//end of RecordBoilerInfo }

 
 
実行結果:
Logging Info:

Temparature 100
Pressure: 12

Message: O. K