.NET 扩展 .NET 扩展实现代码
人气:0想了解.NET 扩展实现代码的相关内容吗,在本文为您仔细讲解.NET 扩展的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:.NET,扩展,下面大家一起来学习吧。
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");
}
}
加载全部内容