/// <summary> /// Returns a string of all the field value pairs of a given object. /// Works only on non-statics. /// </summary> /// <param name="instanc"></param> /// <param name="separator"></param> /// <returns></returns> public static string ObjectToString(object instanc, string separator, ObjectToStringTypes type) { FieldInfo[] fi = instanc.GetType() .GetFields(); string output = string.Empty; if (type == ObjectToStringTypes.Properties || type == ObjectToStringTypes.PropertiesAndFields) { foreach (PropertyInfo property in instanc.GetType() .GetProperties()) { try { output += property.Name + ":" + property.GetValue(instanc, null) + separator; } catch { output += property.Name + ": n/a" + separator; } } } if (type == ObjectToStringTypes.Fields || type == ObjectToStringTypes.PropertiesAndFields) { foreach (FieldInfo field in fi) { try { output = output + field.Name + ": " + field.GetValue(instanc) + separator; } catch { output = output + field.Name + ": n/a" + separator; } } } return(output); }
/// <summary> /// Returns a string of all the field value pairs of a given object. /// Works only on non-statics. /// </summary> /// <param name="instance"></param> /// <param name="separator"></param> /// <returns></returns> public static string ObjectToString(object instance, string separator, ObjectToStringTypes type) { var fi = instance.GetType().Fields(); string output = string.Empty; if (type == ObjectToStringTypes.Properties || type == ObjectToStringTypes.PropertiesAndFields) { foreach (var property in instance.GetType().Properties()) { try { output += property.Name + ":" + instance.GetPropertyValue(property.Name).ToString() + separator; } catch { output += property.Name + ": n/a" + separator; } } } if (type == ObjectToStringTypes.Fields || type == ObjectToStringTypes.PropertiesAndFields) { foreach (var field in fi) { try { output = output + field.Name + ": " + instance.GetFieldValue(field.Name).ToString() + separator; } catch { output = output + field.Name + ": n/a" + separator; } } } return(output); }
/// <summary> /// Returns a string of all the field value pairs of a given object. /// Works only on non-statics. /// </summary> /// <param name="Obj"></param> /// <param name="Separator"></param> /// <returns></returns> public static string ObjectToString(object Obj, string Separator, ObjectToStringTypes Type) { FieldInfo[] fi = Obj.GetType().GetFields(); string lcOutput = ""; if (Type == ObjectToStringTypes.Properties || Type == ObjectToStringTypes.PropertiesAndFields) { foreach (PropertyInfo Property in Obj.GetType().GetProperties()) { try { lcOutput = lcOutput + Property.Name + ":" + Property.GetValue(Obj, null).ToString() + Separator; } catch { lcOutput = lcOutput + Property.Name + ": n/a" + Separator; } } } if (Type == ObjectToStringTypes.Fields || Type == ObjectToStringTypes.PropertiesAndFields) { foreach (FieldInfo Field in fi) { try { lcOutput = lcOutput + Field.Name + ": " + Field.GetValue(Obj).ToString() + Separator; } catch { lcOutput = lcOutput + Field.Name + ": n/a" + Separator; } } } return(lcOutput); }
/// <summary> /// Converts to string. /// </summary> /// <param name="pObject">The p object.</param> /// <param name="pSeparator">The p separator.</param> /// <param name="pType">Type of the p.</param> /// <returns>System.String.</returns> public static string ObjectToString(this object pObject, string pSeparator, ObjectToStringTypes pType) { var fi = pObject.GetType().GetFields(); var output = string.Empty; if ((pType == ObjectToStringTypes.Properties) || (pType == ObjectToStringTypes.PropertiesAndFields)) { foreach (var property in pObject.GetType().GetProperties()) { try { output += $"{property.Name}:{property.GetValue(pObject, null)}{pSeparator}"; } catch { output += $"{property.Name}: n/a{pSeparator}"; } } } if ((pType == ObjectToStringTypes.Fields) || (pType == ObjectToStringTypes.PropertiesAndFields)) { foreach (var field in fi) { try { output = $"{output}{field.Name}: {field.GetValue(pObject)}{pSeparator}"; } catch { output = $"{output}{field.Name}: n/a{pSeparator}"; } } } return(output); }
/// <summary> /// Returns a string of all the field value pairs of a given object. /// Works only on non-statics. /// </summary> /// <param name="Obj"></param> /// <param name="Separator"></param> /// <returns></returns> public static string ObjectToString(object Obj, string Separator, ObjectToStringTypes Type) { FieldInfo[] fi = Obj.GetType().GetFields(); string lcOutput = ""; if (Type == ObjectToStringTypes.Properties || Type == ObjectToStringTypes.PropertiesAndFields) { foreach (PropertyInfo Property in Obj.GetType().GetProperties()) { try { lcOutput = lcOutput + Property.Name + ":" + Property.GetValue(Obj, null).ToString() + Separator; } catch { lcOutput = lcOutput + Property.Name + ": n/a" + Separator; } } } if (Type == ObjectToStringTypes.Fields || Type == ObjectToStringTypes.PropertiesAndFields) { foreach (FieldInfo Field in fi) { try { lcOutput = lcOutput + Field.Name + ": " + Field.GetValue(Obj).ToString() + Separator; } catch { lcOutput = lcOutput + Field.Name + ": n/a" + Separator; } } } return lcOutput; }
/// <summary> /// Returns a string of all the field value pairs of a given object. /// Works only on non-statics. /// </summary> /// <param name="instance"></param> /// <param name="separator"></param> /// <returns></returns> public static string ObjectToString(object instance, string separator, ObjectToStringTypes type) { var fi = instance.GetType().GetFields(); string output = string.Empty; if (type == ObjectToStringTypes.Properties || type == ObjectToStringTypes.PropertiesAndFields) { foreach (var property in instance.GetType().GetProperties()) { try { output += property.Name + ":" + property.GetValue(instance, null).ToString() + separator; } catch { output += property.Name + ": n/a" + separator; } } } if (type == ObjectToStringTypes.Fields || type == ObjectToStringTypes.PropertiesAndFields) { foreach (var field in fi) { try { output = output + field.Name + ": " + field.GetValue(instance).ToString() + separator; } catch { output = output + field.Name + ": n/a" + separator; } } } return output; }