zoukankan      html  css  js  c++  java
  • Reflective implementation of ToString using customer attribute

    There is one class as following:

    ToString before refactor
    public class OldDog
    {
    public OldDog() { }

    public OldDog(string name, string host, string category)
    {
    this.name = name;
    this.host = host;
    this.category = category;
    }

    private string name;
    private string host;
    private string category;

    public string Name
    {
    get { return this.name; }
    set { this.name = value; }
    }

    public string Host
    {
    get { return this.host; }
    set { this.host = value; }
    }

    public string Category
    {
    get { return this.category; }
    set { this.category = value; }
    }

    public override string ToString()
    {
    StringBuilder result
    = new StringBuilder();

    result.Append(
    "<OldDog>");
    result.Append(
    "Name: " + Name);
    result.Append(
    "Host: " + Host);
    result.Append(
    "Category: " + Category);
    result.Append(
    "</OldDog>");

    return result.ToString();
    }
    }

    After refactor

    Improved code
    public class Dog
    {
    public Dog() { }

    public Dog(string name, string host, string category)
    {
    this.name = name;
    this.host = host;
    this.category = category;
    }

    private string name;
    private string host;
    private string category;

    [IsOutputAttribute(
    true)]
    public string Name
    {
    get { return this.name; }
    set { this.name = value; }
    }

    [IsOutputAttribute(
    false)]
    public string Host
    {
    get { return this.host; }
    set { this.host = value; }
    }

    [IsOutputAttribute(
    true)]
    public string Category
    {
    get { return this.category; }
    set { this.category = value; }
    }

    public override string ToString()
    {
    StringBuilder result
    = new StringBuilder();
    PropertyInfo[] lst
    = this.GetType().GetProperties();

    int col = 0;
    foreach (PropertyInfo p in lst)
    {
    foreach (Attribute a in Attribute.GetCustomAttributes(p))
    {
    if (a.GetType() == typeof(IsOutputAttribute)
    && ((IsOutputAttribute)a).IsOutput)
    {
    if (col > 0)
    {
    result.Append(
    "<br />");
    }

    result.Append(p.Name.ToString()
    +": " + p.GetValue(this, null));

    col
    ++;
    }
    }
    }

    return result.ToString();
    }
    }

    public class IsOutputAttribute : Attribute
    {
    protected bool isOutput;

    public bool IsOutput
    {
    get { return this.isOutput; }
    set { this.isOutput = value; }
    }

    public IsOutputAttribute(bool isOutput)
    {
    this.isOutput = isOutput;
    }
    }
    not enough, let's continue
    Final code
    public class Dog
    {
    public Dog() { }

    public Dog(string name, string host, string category)
    {
    this.name = name;
    this.host = host;
    this.category = category;
    }

    private string name;
    private string host;
    private string category;

    [IsOutputAttribute(
    true)]
    public string Name
    {
    get { return this.name; }
    set { this.name = value; }
    }

    [IsOutputAttribute(
    false)]
    public string Host
    {
    get { return this.host; }
    set { this.host = value; }
    }

    [IsOutputAttribute(
    true)]
    public string Category
    {
    get { return this.category; }
    set { this.category = value; }
    }

    public override string ToString()
    {
    return SystemUtility.CustomerToString(this);
    }
    }

    public class IsOutputAttribute : Attribute
    {
    protected bool isOutput;

    public bool IsOutput
    {
    get { return this.isOutput; }
    set { this.isOutput = value; }
    }

    public IsOutputAttribute(bool isOutput)
    {
    this.isOutput = isOutput;
    }
    }

    public static class SystemUtility
    {
    public static string CustomerToString(Object obj)
    {

    StringBuilder result
    = new StringBuilder();
    PropertyInfo[] lst
    = obj.GetType().GetProperties();

    int col = 0;
    foreach (PropertyInfo p in lst)
    {
    foreach (Attribute a in Attribute.GetCustomAttributes(p))
    {
    if (a.GetType() == typeof(IsOutputAttribute)
    && ((IsOutputAttribute)a).IsOutput)
    {
    if (col > 0)
    {
    result.Append(
    "<br />");
    }

    result.Append(p.Name.ToString()
    + ": " + p.GetValue(obj, null));

    col
    ++;
    }
    }
    }

    return result.ToString();
    }
    }



  • 相关阅读:
    前端网站汇总
    更换Sublime Text主题字体
    免费收录网站搜索引擎登录口
    IE6,7,8支持css圆角
    CSS继承—深入剖析
    JavaScript正则表达式大全
    CSS伪元素选择器
    line-height用法总结
    判断腾讯QQ是否在线
    text-overflow使用文字超多div的宽度或超过在table中<td>
  • 原文地址:https://www.cnblogs.com/yang_sy/p/1772896.html
Copyright © 2011-2022 走看看