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

 



相关文章:

  • 2021-05-05
  • 2021-08-14
  • 2021-10-25
  • 2021-08-16
  • 2022-12-23
  • 2021-06-16
  • 2022-02-25
  • 2021-08-03
猜你喜欢
  • 2021-09-11
  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
  • 2021-05-20
  • 2021-06-25
相关资源
相似解决方案