欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.NET 扩展实现代码

程序员文章站 2022-05-19 21:04:53
class command { public virtual void execute() { } } class invalidoperationexception<...
class command
{
public virtual void execute() { }
}

class invalidoperationexception<t> : invalidoperationexception
where t : command
{
public invalidoperationexception(string message) : base(message) { }
// some specific information about
// the command type t that threw this exception
}

static class commandextensions
{
public static void throwinvalidoperationexception<tcommand>(
this tcommand command, string message)
where tcommand : command
{
throw new invalidoperationexception<tcommand>(message);
}
}

class copycommand : command
{
public override void execute()
{
// after something went wrong:
this.throwinvalidoperationexception("something went wrong");
}
}

class cutcommand : command
{
public override void execute()
{
// after something went wrong:
this.throwinvalidoperationexception("something else went wrong");
}
}