/// <summary> /// This function returns a String with each property and value in the class. The String displays the property name, property friendly name and property value. /// </summary> /// <param name="delimiter">What delimiter do you want between each property. Defaults to comma. Could use Envoirnment.NewLine, etc.</param> /// <param name="sortByProperytName">Normally sorts the output by property name.</param> /// <returns>String</returns> public String ToClassString(String delimiter = GlobalConstants.STRING_DEFAULT_DELIMITER, SortByPropertyName sortByProperytName = SortByPropertyName.Yes) { return(ClassMessageCreationHelper.ClassToString(this, delimiter, sortByProperytName)); }
/// <summary> /// Returns a String with each property and value in the class. The String displays the property name, property friendly name and property value. /// </summary> /// <typeparam name="T">Class Type</typeparam> /// <param name="obj">Instance of the class</param> /// <param name="delimiter">What delimiter do you want between each property. Defaults to comma. Could use vbcrlf, etc.</param> /// <param name="sortByPropertyName">Normally sorts the output by property name. To leave in ordinal order, set to False</param> public static String ClassToString <T>(T obj, String delimiter = GlobalConstants.STRING_DEFAULT_DELIMITER, SortByPropertyName sortByPropertyName = SortByPropertyName.Yes) { var sb = new StringBuilder(4096); var list = new List <SortablePropertyBasket>(); foreach (PropertyInfo prop in obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { Object[] auditAttributes = prop.GetCustomAttributes(typeof(AuditAttribute), false); AuditAttribute auditAttribute = null; if (auditAttributes.Length == 1) { auditAttribute = auditAttributes[0] as AuditAttribute; } Int32 auditSequence = 1; if (auditAttribute != null) { auditSequence = auditAttribute.AuditSequence; } list.Add(prop.GetValue(obj, null) != null ? new SortablePropertyBasket(auditSequence, prop.Name, CamelCaseString.GetWords(prop.Name), prop.GetValue(obj, null).ToString()) : new SortablePropertyBasket(auditSequence, prop.Name, CamelCaseString.GetWords(prop.Name), _NULL)); } if (list.Count > 0) { if (sortByPropertyName == SortByPropertyName.Yes) { list.Sort(); } foreach (SortablePropertyBasket propertyBasket in list) { sb.Append(propertyBasket.ToString()); sb.Append(delimiter); } if (sb.Length > delimiter.Length) { sb.Length -= delimiter.Length; } } else { sb.Append(Resources.ClassMessageCreationHelper_ClassToString_Class_has_no_properties); } return(sb.ToString()); }
/// <summary> /// Used to generate an Dictionary(Of String, String) for each property in the class. /// </summary> /// <param name="defaultValue">Default message if no class propeties are decorated</param> /// <param name="dictionary">Pass an IDictionary Object that needs to be populated. Typicaly this would be the Data property of an Exception Object.</param> /// <param name="sortByPropertyName">Normally sorts the output by property name.</param> /// <returns>IDictionary</returns> public IDictionary <String, String> ToClassIDictionary(String defaultValue, IDictionary <String, String> dictionary, SortByPropertyName sortByPropertyName = SortByPropertyName.Yes) { return(ClassMessageCreationHelper.ClassToIDictionary(this, defaultValue, dictionary, sortByPropertyName)); }
/// <summary> /// Used to generate a Dictionary(Of String, String) for each property in the entity. Dictionary is property name, property value. This method signature allows a IDictionary Object to be passed in. This feature is useful when generating a name value pair dictionary to store in an Exception Object's Data property. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj">The obj.</param> /// <param name="defaultValue">The default value.</param> /// <param name="dictionary">The dictionary.</param> /// <param name="sortByPropertyName">Name of the sort by property.</param> /// <returns></returns> public static IDictionary <String, String> ClassToIDictionary <T>(T obj, String defaultValue, IDictionary <String, String> dictionary, SortByPropertyName sortByPropertyName = SortByPropertyName.Yes) { var list = new List <SortablePropertyBasket>(); foreach (PropertyInfo prop in obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { Object[] auditAttributes = prop.GetCustomAttributes(typeof(AuditAttribute), false); AuditAttribute auditAttribute = null; if (auditAttributes.Length == 1) { auditAttribute = auditAttributes[0] as AuditAttribute; } if (auditAttribute != null) { list.Add(new SortablePropertyBasket(_ONE, prop.Name, String.Empty, prop.GetValue(obj, null).ToString())); } } if (list.Count > 0) { if (sortByPropertyName == SortByPropertyName.Yes) { list.Sort(); } foreach (SortablePropertyBasket propertyBasket in list) { dictionary.Add(propertyBasket.PropertyName, propertyBasket.Value); } } else { dictionary.Add(_DEFAULTVALUE, defaultValue); } return(dictionary); }
/// <summary> /// Used to generate a Dictionary(Of String, String) for each property in the entity. Dictionary is property name, property value. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj">The obj.</param> /// <param name="defaultValue">The default value.</param> /// <param name="sortByPropertyName">Name of the sort by property.</param> /// <returns></returns> public static IDictionary <String, String> ClassToIDictionary <T>(T obj, String defaultValue, SortByPropertyName sortByPropertyName = SortByPropertyName.Yes) { var dictionary = new Dictionary <String, String>(); return(ClassToIDictionary(obj, defaultValue, dictionary, sortByPropertyName)); }
/// <summary> /// Builds up a String containing each property and value in the class. The String displays the property name, property friendly name and property value. /// </summary> /// <param name="delimiter">What delimiter do you want between each property? Defaults to comma; can pass others like Environment.NewLine, etc.</param> /// <param name="sortByPropertyName">If set to <c>SortByPropertyName.Yes</c> then output will be sorted by AuditAttribute.AuditSequence and then property name; otherwise no additional sorting is performed and properties; will be left in ordinal order.</param> /// <returns>A String containing each property name, friendly name and value, separated by the delimiter and optionally sorted by property name.</returns> public String ToClassString(String delimiter = GlobalConstants.DefaultDelimiter, SortByPropertyName sortByPropertyName = SortByPropertyName.Yes) { return(ClassMessageCreationHelper.ClassToString(this, delimiter, sortByPropertyName)); }
/// <summary> /// Builds up a string containing each property and value in the class. The string displays the property name, property friendly name and property value. /// </summary> /// <typeparam name="T">Class Type</typeparam> /// <param name="instance">Instance of the class</param> /// <param name="delimiter">What delimiter do you want between each property? Defaults to comma; can pass others like Environment.NewLine, etc.</param> /// <param name="sortByPropertyName">If set to <c>SortByPropertyName.Yes</c> then output will be sorted by AuditAttribute.AuditSequence and then property name; otherwise no additional sorting is performed and properties; will be left in ordinal order.</param> /// <returns>A string containing each property name, friendly name and value, separated by the delimiter and optionally sorted by property name.</returns> public static String ClassToString <T>(T instance, String delimiter = GlobalConstants.DefaultDelimiter, SortByPropertyName sortByPropertyName = SortByPropertyName.Yes) { var sb = new StringBuilder(4096); var list = new List <SortablePropertyBasket>(); foreach (var propInfo in PclReflection.GetPropertiesInfo(instance)) { var auditAttributes = propInfo.GetCustomAttributes <AuditAttribute>(false); var auditAttributeList = new List <AuditAttribute>(auditAttributes); AuditAttribute auditAttribute = null; if (auditAttributeList.Count == 1) { auditAttribute = auditAttributeList[0]; } var auditSequence = 1; if (auditAttribute != null) { auditSequence = auditAttribute.AuditSequence; } if (propInfo.Name != "Item" && propInfo.Name != "ValidationErrors") { list.Add(propInfo.GetValue(instance, null) != null ? new SortablePropertyBasket(auditSequence, propInfo.Name, CamelCaseString.GetWords(propInfo.Name), propInfo.GetValue(instance, null).ToString()) : new SortablePropertyBasket(auditSequence, propInfo.Name, CamelCaseString.GetWords(propInfo.Name), Null)); } } if (list.Count > 0) { if (sortByPropertyName == SortByPropertyName.Yes) { list.Sort(); } foreach (var propertyBasket in list) { sb.Append(propertyBasket); sb.Append(delimiter); } if (sb.Length > delimiter.Length) { sb.Length -= delimiter.Length; } } else { sb.Append("Class has no properties"); } return(sb.ToString()); }
/// <summary> /// Populates the passed in IDictionary with property's name and value in the class for properties decorated with the <see cref="AuditAttribute"/>. /// </summary> /// <typeparam name="T">Class type.</typeparam> /// <param name="instance">The instance.</param> /// <param name="defaultValue">If no class properties are decorated with the <see cref="AuditAttribute"/> then a single entry will be added to the dictionary that is named 'DefaultValue' and will have the value of defaultValue.</param> /// <param name="dictionary">Pass an IDictionary object that needs to be populated. This could be the Data property of an exception object that you want to populate, etc.</param> /// <param name="sortByPropertyName">If set to <c>SortByPropertyName.Yes</c> then output will be sorted by AuditAttribute.AuditSequence and then property name; otherwise no additional sorting is performed and properties; will be left in ordinal order.</param> /// <returns>The dictionary passed in populated with properties and values.</returns> public static IDictionary <String, String> ClassToIDictionary <T>(T instance, String defaultValue, IDictionary <String, String> dictionary, SortByPropertyName sortByPropertyName = SortByPropertyName.Yes) { var list = new List <SortablePropertyBasket>(); foreach (var propInfo in PclReflection.GetPropertiesInfo(instance)) { var auditAttributes = propInfo.GetCustomAttributes <AuditAttribute>(false); var auditAttributeList = new List <AuditAttribute>(auditAttributes); AuditAttribute auditAttribute = null; if (auditAttributeList.Count == 1) { auditAttribute = auditAttributeList[0]; } if (auditAttribute != null) { list.Add(new SortablePropertyBasket(One, propInfo.Name, String.Empty, propInfo.GetValue(instance, null).ToString())); } } if (list.Count > 0) { if (sortByPropertyName == SortByPropertyName.Yes) { list.Sort(); } foreach (SortablePropertyBasket propertyBasket in list) { dictionary.Add(propertyBasket.PropertyName, propertyBasket.Value); } } else { dictionary.Add(DefaultValue, defaultValue); } return(dictionary); }