.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");
}
}
{
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");
}
}
上一篇: dz asp.net论坛中函数--根据Url获得源文件内容
下一篇: 喝多为啥要扛着自行车走