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();
}
}

 



posted @ 2010-07-07 15:22  On the road....  阅读(274)  评论(0编辑  收藏  举报