How can i create infinite property
In the C# , string has infinite property like this
string a = "";
a.ToString().Length.ToString().ToUpper().ToLower().ToString()....
and how can i create like this
ClassName a = new ClassName();
a.text("message").title("hello").icon("user");
thanks, it's work
public class Modal { public Modal() { } private string Date = null; private string Text = null; private string Title = null; private string Icon = null; private string Subtitle = null; private string Confirm = "ok"; private string Cancel = "cancel"; private string Type = "warning"; public Modal text(string text) { this.Text = text; return this; } public Modal title(string title) { this.Title = title; return this; } public Modal icon(string icon) { this.Icon = icon; return this; } public Modal subtitle(string subtitle) { this.Subtitle = subtitle; return this; } public Modal confirm(string confirm) { this.Confirm = confirm; return this; } public Modal cancel(string cancel) { this.Cancel = cancel; return this; } public Modal type(string type) { this.Type = type; return this; } public void show(System.Web.UI.Page Page) { StringBuilder s = new StringBuilder(); s.Append("{'date':'" + (DateTime.UtcNow.Ticks - 621355968000000000).ToString() + "','text':'" + Text + "','title':'" + Title + "','icon':'" + Icon + "','subtitle':'" + Subtitle + "','confirm':'" + Confirm + "','cancel':'" + Cancel + "','type':'" + Type + "'}"); 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);
result
Every one of those methods on string
simply return a string
. (Well, almost. You have a .Length
in there which isn't correct.) If you return your object from your methods, you can achieve the same concept. (In some cases this can be referred to as a "fluent syntax", though the string
example isn't necessarily really that.)
For example, say your .Title()
method is as such:
class ClassName
{
//...
public ClassName Title(string title)
{
this.Title = title;
return this;
}
}
Then any time you call someObj.Title("some string")
that method will return the object itself:
var someObj = new ClassName();
someObj.Title("some title").SomeOtherOperation();
It's not "infinite", it's just a method that returns the same type on which it's invoked. It can return itself or any instance of that type. Just be sure to pay attention to the interface you're building when you do this, because you could accidentally create fairly unintuitive things. (Fluent chains which produce unintended side-effects on the original object or don't produce intended effects on the original object.)
链接地址: http://www.djcxy.com/p/68920.html上一篇: 如何控制gridExtra中的绘图宽度?
下一篇: 我怎样才能创造无限的财产