Revisions

gist: 111152 Download_button fork
public
Public Clone URL: git://gist.github.com/111152.git
Embed All Files: show embed
C# #
1
2
3
4
5
6
7
8
9
10
11
public static object GetPropertyValue(this object obj, string propertyName)
{
    try
    {
        return obj.GetType().GetProperty(propertyName).GetValue(obj, null);
    }
    catch
    {
        return null;
    }
}
C# #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public string CriaLink(string titulo, object parametros)
{
    var para = (string) parametros.GetPropertyValue("Para");
    var descricao = (string) parametros.GetPropertyValue("Descricao");
    var borda = (int?) parametros.GetPropertyValue("Borda");
 
    var linkBuilder = new StringBuilder();
 
    linkBuilder.Append("<a");
 
    if(!String.IsNullOrEmpty(para))
    {
        linkBuilder.AppendFormat(" href='{0}'", para);
    }
 
    if(!String.IsNullOrEmpty(descricao))
    {
        linkBuilder.AppendFormat(" title='{0}'", descricao);
    }
 
    if(borda != null && borda > 0)
    {
        linkBuilder.AppendFormat(" style='border: {0}px solid silver'", borda);
    }
 
    linkBuilder.AppendFormat(">{0}</a>", titulo);
 
    return linkBuilder.ToString();
}
C# #
1
2
3
4
5
6
7
8
CriaLink("Google", new { Para = "http://www.google.com", Descricao = "Clique aqui para acessar o Google.", Borda = 2 });
// <a href='http://www.google.com' title='Clique aqui para acessar o Google.' style='border: 2px solid silver'>Google</a>
 
CriaLink("Globo", new { Para = "http://www.globo.com", Borda = 2 });
// <a href='http://www.globo.com' style='border: 2px solid silver'>Globo</a>
 
CriaLink("Home", new { Para = "/Index.html", Descricao = "Voltar para a página inicial." });
// <a href='/Index.html' title='Voltar para a página inicial.'>Home</a>