读书人

C#其它类的静态成员函数的代理怎么实现

发布时间: 2013-01-11 11:57:35 作者: rapoo

C#其它类的静态成员函数的代理如何实现?
C#其它类的静态成员函数的代理如何实现?
一定需要类的实例吗?

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);

}
}
}

------解决方案--------------------


delegate就是委托,而不是什么“代理”,你看的书翻译的有问题。

读书人网 >C#

热点推荐