C#实现jQuery的方法连缀
jQuery的方法连缀使用起来非常方便,可以简化语句,让代码变得清晰简洁。那C#的类方法能不能也实现类似的功能呢?基于这样的疑惑,研究了一下jQuery的源代码,发现就是需要方法连缀的函数方法最后返回对象本身即可。既然javascript可以,C#应该也是可以的。
为了验证,编写一个jQPerson类,然后用方法连缀对其ID,Name,Age等属性进行设置,请看下面的代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace CSharpMethodLikeJQuery 8 { 9 public class jQPerson 10 { 11 string Id { set; get; } 12 string Name { set; get; } 13 int Age { set; get; } 14 string Sex { set; get; } 15 string Info { set; get; } 16 17 public jQPerson() 18 { 19 20 } 21 /// <summary> 22 /// 设置ID,返回this,即jQPerson实例 23 /// </summary> 24 /// <param name="Id"></param> 25 /// <returns></returns> 26 public jQPerson setId(string Id) 27 { 28 this.Id = Id; 29 return this; 30 } 31 /// <summary> 32 /// 返回this,即jQPerson实例 33 /// </summary> 34 /// <param name="name"></param> 35 /// <returns></returns> 36 public jQPerson setName(string name) 37 { 38 39 this.Name = name; 40 return this; 41 } 42 /// <summary> 43 /// 返回this,即jQPerson实例 44 /// </summary> 45 /// <param name="age"></param> 46 /// <returns></returns> 47 public jQPerson setAge(int age) 48 { 49 50 this.Age = age; 51 return this; 52 } 53 /// <summary> 54 /// 返回this,即jQPerson实例 55 /// </summary> 56 /// <param name="sex"></param> 57 /// <returns></returns> 58 public jQPerson setSex(string sex) 59 { 60 61 this.Sex = sex; 62 return this; 63 } 64 /// <summary> 65 /// 返回this,即jQPerson实例 66 /// </summary> 67 /// <param name="info"></param> 68 /// <returns></returns> 69 public jQPerson setInfo(string info) 70 { 71 72 this.Info = info; 73 return this; 74 } 75 /// <summary> 76 /// tostring输出键值对信息 77 /// </summary> 78 /// <returns></returns> 79 public string toString() 80 { 81 82 return string.Format("Id:{0},Name:{1},Age:{2},Sex:{3},Info:{4}", this.Id, this.Name, this.Age, this.Sex, this.Info); 83 84 85 } 86 87 } 88 }
然后可以对上面进行测试,看方法连缀是否生效:
1 /// <summary> 2 ///toString 的测试 3 ///</summary> 4 [TestMethod()] 5 public void toStringTest() 6 { 7 jQPerson target = new jQPerson(); 8 target.setId("2") 9 .setName("jack") 10 .setAge(26) 11 .setSex("man") 12 .setInfo("ok"); 13 string expected = "Id:2,Name:jack,Age:26,Sex:man,Info:ok"; 14 string actual; 15 actual = target.toString(); 16 Assert.AreEqual(expected, actual); 17 //Assert.Inconclusive("验证此测试方法的正确性。"); 18 }
可以看到,方法连缀确实可以让代码变得直观和简洁,增加可阅读性。
C#实现jQuery的方法连缀相关推荐
- jQuery 事件方法大全
Dom : Attribute:$("p").addClass(css中定义的样式类型); 给某个元素添加样式$("img").attr({src:" ...
- jQuery on()方法
jQuery on()方法是官方推荐的绑定事件的一个方法. $(selector).on(event,childSelector,data,function,map) 由此扩展开来的几个以前常见的方法 ...
- JQuery Attributes 方法说明
转:http://blog.sina.com.cn/s/blog_5ea2cb7e0100c4cx.html JQuery Attributes 方法说明(2009-02-17 01:02:33) 标 ...
- Jquery getJSON方法分析(一)
准备工作 ·Customer类 public class Customer { public int Unid { get; set; } public string Customer ...
- jQuery 遍历方法
jQuery 遍历方法 方法 描述 add() 把元素添加到匹配元素的集合中 addBack() 把之前的元素集添加到当前集合中 andSelf() 在版本 1.8 中被废弃.addBack() 的别 ...
- jquery ajax html方法吗,jQuery ajax方法
jQuery AJAX 方法 jQuery load() 方法是简单强大的AJAX方法.从服务器加载数据,并将数据显示在被选中元素中. 语法: $(selector).load(URL,data,ca ...
- jquery订阅发布插件代码草稿,为jquery扩展jquery.publish,jquery.subscribe方法
jquery订阅发布插件代码草稿! jquery订阅发布插件代码草稿,为jquery扩展jquery.publish,jquery.subscribe方法 --- jquery订阅发布插件代码草稿 & ...
- jQuery Css方法
jQuery css() 方法 css() 方法设置或返回被选元素的一个或多个样式属性. 返回 CSS 属性 如需返回指定的 CSS 属性的值,请使用如下语法: css("propertyn ...
- jQuery on()方法绑定动态元素的点击事件无响应的解决办法
$('#check_all').on('click' , function(){ alert(1); }); $("#yujinlist").append(html); count ...
最新文章
- MySQL:一个死锁分析 (未分析出来的死锁)
- mysql 开启远程
- CSS样式优先级与权重计算方式
- 微信端上传图片方式1
- 华为锁屏后微信无法连接服务器,怎么解决华为p9锁屏收不到微信信息【教程详解】...
- HQ-day2 C#语言基础
- 小白开学Asp.Net Core 《七》
- OpenCL 第6课:矩阵转置
- ipqc异常处理流程图_IPQC巡检流程.七大手法.八大原则.九大步骤
- mysql explain insert_mysql explain详解
- 【Python成长之路】词云图制作
- OpenCV学习笔记04:在Visual Studio上使用OpenCV4.5.5
- 《Windows高级调试》译者线上聊天活动获奖网友名单
- MFC 教程【11_MFC下的文件类 】
- Linux 内存管理(一)——地址空间
- 如何打开mo文件并修改 PoEdit
- 车载电子瞬态浪涌保护用瞬态抑制TVS二极管,如何正确选型?
- 审视AI界的“SOTA成瘾”丨AI学者万字论述
- 【论文汇总】人群计数中Transformer的应用,持续更新
- 复现Dense Extreme Inception Network(pytorch)