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

jQuery clickable div with working mailto link inside -

java - Getting corefrences with Standard corenlp package -

WPF: binding viewmodel property of type DateTime to Calendar inside ItemsControl -