如下代码所示
以上代码就是使用了扩展方法进行链式编程,从而使得整个请求信息可以在一句代码中体现出来
接下来,我们自己实现链式代码
public static class ContextExtension
{
public static RectangleContext SetLength(this RectangleContext context,int num)
{
RectangleContext.Config.Length = num;
return context;
}
public static RectangleContext SetWideth(this RectangleContext context, int num)
{
RectangleContext.Config.Wideth = num;
return context;
}
public static RectangleContext SetHeight(this RectangleContext context, int num)
{
RectangleContext.Config.Height = num;
return context;
}
}
public class RectangleContext
{
public static RectangleContext Config=new RectangleContext();
public int Length { get; set; }
public int Wideth { get; set; }
public int Height { get; set; }
}
调用和执行结果
总结
1.使用扩展方法能在不修改原有类型的基础下,动态添加方法,这使得整个框架更具有灵活性
2.在使用上下文信息的时候,可以使用链式编程,使得调用时能够在一句代码中完成所有属性设置












