C#其它类的静态成员函数的代理如何实现?

JiangXiang 2013-01-01 02:39:35
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);

}
}
}
...全文
188 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
JiangXiang 2013-01-02
  • 打赏
  • 举报
回复
Conventions The following important conventions are used with events: Event Handlers in the .NET Framework return void and take two parameters. The first paramter is the source of the event; that is the publishing object. The second parameter is an object derived from EventArgs. Events are properties of the class publishing the event. The keyword event controls how the event property is accessed by the subscribing classes.
JiangXiang 2013-01-02
  • 打赏
  • 举报
回复
感谢您的回答,是我自己把代码看错了。
threenewbee 2013-01-01
  • 打赏
  • 举报
回复
delegate就是委托,而不是什么“代理”,你看的书翻译的有问题。

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧