Advertisement
stefanpu

GSM ToString method

Feb 25th, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.17 KB | None | 0 0
  1.     public override string ToString()
  2.         {
  3.             Type thisType = this.GetType();
  4.             // You must specify the Instance and Public flags to get the public
  5.             //instance properties.
  6.             //See:
  7.             //Note: if take the static IPhone4S you get stackoverflow exception!!!
  8.             PropertyInfo[] properties =
  9.                 thisType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
  10.  
  11.             //This code is in separate private method "AppendPropertiesNamesAndValues(properties)"
  12.             //-------------------------------------------------------------------------
  13.             StringBuilder phoneInfo = new StringBuilder();
  14.            
  15.             phoneInfo.AppendLine("/-----Phone Info-----/\n");
  16.             foreach (var property in properties)
  17.             {
  18.                 var propertyValue = property.GetValue(this) ?? "null";
  19.                 phoneInfo.AppendLine(property.Name + ": " + propertyValue);
  20.             }
  21.             phoneInfo.AppendLine("/-------------------/");
  22.             //-------------------------------------------------------------------------
  23.  
  24.             return phoneInfo.ToString();
  25.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement