日期:2014-05-17 浏览次数:20965 次
using System;
using System.IO;
namespace Akadia.SimpleDelegate
{
// Delegate Specification
public class MyClass
{
// Declare a delegate that takes a single string parameter
// and has no return type.
public delegate void LogHandler(string message);
// The use of the delegate is just like calling a function directly,
// though we need to add a check to see if the delegate is null
// (that is, not pointing to a function) before calling the function.
public void Process(LogHandler logHandler)
{
if (logHandler != null)
{
logHandler("Process() begin");
}
if (logHandler != null)
{
logHandler("Process() end");
}
}
}
// The FileLogger class merely encapsulates the file I/O
public class FileLogger
{
// Member Function which is used in the Delegate
public static void Logger(string s)
{
}
}
public class TestApplication
{
static void Main(string[] args)
{
MyClass myClass = new MyClass();
MyClass.LogHandler myLogger = new MyClass.LogHandler(FileLogger.Logger);
myClass.Process(myLogger);
}