【发布时间】:2016-11-14 15:38:15
【问题描述】:
在 C# 中,字符串具有这样的无限属性
string a = "";
a.ToString().Length.ToString().ToUpper().ToLower().ToString()....
我怎样才能像这样创建
ClassName a = new ClassName();
a.text("message").title("hello").icon("user");
谢谢,成功了
公共类模态 { 公共模态() { } 私人字符串日期=空; 私有字符串文本 = null; 私有字符串 Title = null; 私有字符串图标 = null; 私人字符串字幕=空; 私人字符串确认=“确定”; 私人字符串取消=“取消”; 私人字符串类型=“警告”; 公共模态文本(字符串文本){ this.Text = text;返回这个; } 公共模态标题(字符串标题){ this.Title = title;返回这个; } 公共模态图标(字符串图标){ this.Icon = icon;返回这个; } 公共模态字幕(字符串字幕) { this.Subtitle = 副标题; 返回这个; } 公共模态确认(字符串确认){ this.Confirm = 确认;返回这个; } 公共模式取消(字符串取消){ this.Cancel =取消;返回这个; } 公共模态类型(字符串类型){ this.Type = type;返回这个; } 公共无效显示(System.Web.UI.Page 页面) { StringBuilder s = new StringBuilder(); s.Append("{'date':'" + (DateTime.UtcNow.Ticks - 621355968000000000).ToString() + "','text':'" + Text + "','title':'" + Title + "','icon':'" + 图标 + "','subtitle':'" + 字幕 + "','confirm':'" + 确认 + "','cancel':'" + 取消 + " ','类型':'" + 类型 + "'}"); string _script = "showModal(" + s.ToString() + ");"; ScriptManager.RegisterStartupScript(Page, Page.GetType(), (DateTime.UtcNow.Ticks - 621355968000000000).ToString(), _script, true); } }Modal m = new Modal();
m.text("this is text").title("this is title").icon("fa-car").type("danger").show(this);
【问题讨论】:
-
它没有“无限属性”——它只有各种返回
string的方法。如果你让你的方法返回相同的类型,你也可以这样做...... -
您的示例甚至无法编译,因为 Length 是 int 而不是字符串。没有属性,只有链式函数。
-
我不认为你可以做
a.ToString().Length.ToUpper(),因为Length是int.. 正如@JonSkeet 提到的,只要每个方法返回一个string,你就可以继续调用string方法的结果 -
流利的接口/方法链是你需要研究的,例如codeproject.com/Articles/640997/…
标签: c# class properties