Let\'s say I have a string that I retrieve from a DB like:
\"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore e
public class Program
{
static void Main(string[] args)
{
ParserBase parser = new ParserBase();
Console.WriteLine(parser.DynamicRenderControl(parser.Parse("")));
Console.ReadLine();
}
}
public class ParserBase
{
public virtual Dictionary Parse(string stringToParse)
{
//...
// parse the stringToParse
//...
Dictionary parsedPropertiesValues = new Dictionary();
parsedPropertiesValues.Add("NavigateUrl", @"http://www.koolzers.net");
return parsedPropertiesValues;
}
protected virtual void SetProperty(T obj, string propertyName, string value) where T : WebControl
{
typeof(T).GetProperty(propertyName).SetValue(obj, value, null);
}
public string DynamicRenderControl(Dictionary parsedPropertiesValues) where T : WebControl, new()
{
StringBuilder sb = new StringBuilder();
using (T control = new T())
{
foreach (KeyValuePair keyValue in parsedPropertiesValues)
{
SetProperty(control, keyValue.Key, keyValue.Value);
}
using (StringWriter tw = new StringWriter(sb))
{
using (HtmlTextWriter w = new HtmlTextWriter(tw))
{
control.RenderControl(w);
}
}
}
return sb.ToString();
}
}