C# call method on a member class -


i want that:

public class myclass {     public string vara{ get; set; }     public string[] varb{ get; set; }      //.....     public ?? string tohtml()     {         //return html value     } }  public class run() {     myclass c = new myclass();     c.vara = "toto";     c.varb = new string[] { "foo", "bar" };      string = c.vara.tohtml() // -> "<p>toto</p>";     string b = c.varb.tohtml() // -> "<ul><li>foo</li><li>bar</li></ul>"; } 

how can ?

edit: have change run()

this way implement scenario extension methods. while, others have noted, make sense keep logic turn strings html within myclass, in scenarios might make sense use approach.

public class myclass {     public string vara{ get; set; }     public string[] varb{ get; set; }            }  public static class myextensions {     public static string tohtml(this string input, string format)     {         return string.format(format, input);     }      public static string tohtml(this string input)     {         return tohtml(input,"<p>{0}</p>");     }      public static string tohtml(this ienumerable<string> input, string wrapperformat, string elementformat)     {         string body= input                         .select(s => string.format(elementformat,s))                         .aggregate((a,b)=> a+b);         return string.format(wrapperformat,body);     }      public static string tohtml(this ienumerable<string> input)     {         return tohtml(input,"<ul>{0}</ul>","<li>{0}</li>");              } } 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -