/// <summary>Gets the value of a specific property with respect to a null-based (row or column) index. /// </summary> /// <typeparam name="TEnum">The type of the output which is assumed to be a enumeration.</typeparam> /// <param name="rowIndex">The null-based index of the row.</param> /// <param name="columnIndex">The null-based index of the column.</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation.</param> /// <param name="value">The value (output).</param> /// <returns>A value indicating whether <paramref name="value"/> contains valid data.</returns> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represents a enumeration.</exception> public ExcelCellValueState TryGetValue <TEnum>(EnumStringRepresentationUsage enumStringRepresentationUsage, out TEnum value, int rowIndex, int columnIndex) where TEnum : struct, IComparable, IConvertible, IFormattable { if ((rowIndex < m_RowCount) && (rowIndex >= 0) && (columnIndex < m_ColumnCount) && (columnIndex >= 0)) { string valueDropDownListAsString = EnumString <TEnum> .GetValues(enumStringRepresentationUsage).AsExcelDropDownListString(); m_SetOfUsedPropertyIndices.Add(rowIndex); m_Range.CreateDropdownList(rowIndex, columnIndex, valueDropDownListAsString); if (ExcelDataConverter.IsEmptyCell(m_Data[rowIndex, columnIndex]) == true) { value = default(TEnum); m_GuidedExcelDataQuery.SetData(rowIndex, columnIndex, typeof(TEnum)); return(ExcelCellValueState.EmptyOrMissingExcelCell); } else if (ExcelDataConverter.TryGetCellValue <TEnum>(m_Data[rowIndex, columnIndex], enumStringRepresentationUsage, out value) == true) { m_GuidedExcelDataQuery.SetData(rowIndex, columnIndex, value); return(ExcelCellValueState.ProperValue); } } value = default(TEnum); return(ExcelCellValueState.NoValidValue); }
/// <summary>Converts the string representation of an enumeration item to its enumeration item equivalent.</summary> /// <param name="stringRepresentation">The <see cref="System.String"/> representation to search for.</param> /// <param name="enumStringRepresentationUsage">The method how to compute the string representation of the items of the /// enumeration represented by <typeparamref name="TEnum"/>.</param> /// <returns>The element of the enumeration with respect to <paramref name="stringRepresentation"/>.</returns> /// <remarks>White spaces etc. will be ignored.</remarks> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represent an enumeration.</exception> /// <exception cref="FormatException">Thrown, if there is no enumeration item with string representation <paramref name="stringRepresentation"/>.</exception> public static TEnum Parse(string stringRepresentation, EnumStringRepresentationUsage enumStringRepresentationUsage = EnumStringRepresentationUsage.LanguageStringAttribute) { if (TryParse(stringRepresentation, out TEnum value, enumStringRepresentationUsage) == true) { return(value); } throw new FormatException(String.Format(ExceptionMessages.ArgumentIsInvalid, stringRepresentation)); }
/// <summary>Gets the value of a specific Excel cell. /// </summary> /// <typeparam name="TEnum">The type of the enumeration.</typeparam> /// <param name="excelCell">The Excel cell.</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation of the enumeration <typeparamref name="TEnum"/>.</param> /// <param name="value">The value of the <paramref name="excelCell"/> (output).</param> /// <returns>A value indicating whether <paramref name="value"/> contains valid data.</returns> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represent a enumeration.</exception> public static bool TryGetCellValue <TEnum>(object excelCell, EnumStringRepresentationUsage enumStringRepresentationUsage, out TEnum value) where TEnum : struct, IComparable, IConvertible, IFormattable { if (typeof(TEnum).IsEnum) { string tableValue = ((string)excelCell); return(EnumString <TEnum> .TryParse(tableValue, out value, enumStringRepresentationUsage)); } throw new ArgumentException(String.Format(ExceptionMessages.ArgumentIsInvalid, "EnumType: " + typeof(TEnum).ToString()), "TEnum"); }
/// <summary>Gets the value of a specific property with respect to a null-based (row or column) index. /// </summary> /// <typeparam name="TEnum">The type of the output which is assumed to be a enumeration.</typeparam> /// <param name="rowIndex">The null-based index of the row.</param> /// <param name="columnIndex">The null-based index of the column.</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation.</param> /// <param name="value">The value (output).</param> /// <returns>A value indicating whether <paramref name="value"/> contains valid data.</returns> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represents a enumeration.</exception> public ExcelCellValueState TryGetValue <TEnum>(EnumStringRepresentationUsage enumStringRepresentationUsage, out TEnum value, int rowIndex, int columnIndex) where TEnum : struct, IComparable, IConvertible, IFormattable { if (typeof(TEnum).IsEnum == false) { throw new ArgumentException("The type " + typeof(TEnum).ToString() + " does not represents a enumeration.", "TEnum"); } if ((rowIndex >= 0) && (rowIndex < m_RowCount)) { string dropDownListAsString = EnumString <TEnum> .GetValues(enumStringRepresentationUsage).AsExcelDropDownListString(); if (columnIndex == 0) { m_PropertyNames.CreateDropdownList(0, rowIndex, dropDownListAsString); m_SetOfUsedPropertyIndices.Add(rowIndex); if (ExcelDataConverter.IsEmptyCell(m_PropertyNameArray[0, rowIndex]) == true) { value = default(TEnum); m_GuidedExcelDataQuery.SetData(rowIndex, columnIndex, typeof(TEnum)); return(ExcelCellValueState.EmptyOrMissingExcelCell); } if (ExcelDataConverter.TryGetCellValue <TEnum>(m_PropertyNameArray[0, rowIndex], enumStringRepresentationUsage, out value) == true) { m_GuidedExcelDataQuery.SetData(rowIndex, columnIndex, value); return(ExcelCellValueState.ProperValue); } } else if ((columnIndex >= 1) && (columnIndex < m_ColumnCount)) { m_PropertyValues.CreateDropdownList(columnIndex - 1, rowIndex, dropDownListAsString); m_SetOfUsedPropertyIndices.Add(rowIndex); if (ExcelDataConverter.IsEmptyCell(m_PropertyValueArray[columnIndex - 1, rowIndex]) == true) { value = default(TEnum); m_GuidedExcelDataQuery.SetData(rowIndex, columnIndex, typeof(TEnum)); return(ExcelCellValueState.EmptyOrMissingExcelCell); } else if (ExcelDataConverter.TryGetCellValue <TEnum>(m_PropertyValueArray[columnIndex - 1, rowIndex], enumStringRepresentationUsage, out value) == true) { m_GuidedExcelDataQuery.SetData(rowIndex, columnIndex, value); return(ExcelCellValueState.ProperValue); } } } value = default(TEnum); return(ExcelCellValueState.NoValidValue); }
/// <summary>Gets the value of a specific cell. /// </summary> /// <typeparam name="TEnum">The type of the output which is assumed to be a enumeration.</typeparam> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation.</param> /// <param name="rowIndex">The null-based index of the row.</param> /// <param name="columnIndex">The null-based index of the column.</param> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represents a enumeration or no valid input is given by the user.</exception> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> public static TEnum GetValue <TEnum>(this IExcelDataQuery dataQuery, EnumStringRepresentationUsage enumStringRepresentationUsage, int rowIndex = 0, int columnIndex = 0) where TEnum : struct, IComparable, IConvertible, IFormattable { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } TEnum value; if (dataQuery.TryGetValue <TEnum>(enumStringRepresentationUsage, out value, rowIndex, columnIndex) == ExcelCellValueState.ProperValue) { return(value); } throw new ArgumentException("No valid input '" + dataQuery.ToString(rowIndex, columnIndex) + "' at row: " + rowIndex + ", column: " + columnIndex + " found" + GetFormatedDataQueryName(dataQuery) + "."); }
/// <summary>Retrieves a collection of the values of the constants in a specified enumeration in its <see cref="EnumString<TEnum>"/> representation. /// </summary> /// <param name="enumStringRepresentationUsage">The method how to compute the string representation.</param> /// <returns>A collection of <see cref="EnumString<TEnum>"/> objects that contains the items of the enumeration </returns> /// <exception cref="ArgumentException">Thrown if <typeparamref name="TEnum"/> does not represents an enumeration.</exception> public static IEnumerable <EnumString <TEnum> > GetValues(EnumStringRepresentationUsage enumStringRepresentationUsage = EnumStringRepresentationUsage.LanguageStringAttribute) { Type enumerationType = typeof(TEnum); if (enumerationType.IsEnum == false) { throw new ArgumentException(String.Format("The type {0} does not represents an enumeration.", enumerationType.ToString())); } foreach (TEnum value in Enum.GetValues(enumerationType)) { Enum enumValueAsEnum = (Enum)((object)value); // one can not convert it directly! yield return(new EnumString <TEnum>(value, EnumExtensions.ToFormatString(enumValueAsEnum, enumStringRepresentationUsage))); } }
public void CompareTo_StringAndLanguageAttributeFileAccessEnum_RelationOfUnderlyingEnumValues( [Values(StringAndLanguageAttributeFileAccess.Delete, StringAndLanguageAttributeFileAccess.Read, StringAndLanguageAttributeFileAccess.ReadWrite, StringAndLanguageAttributeFileAccess.Write)] Enum value, [Values(StringAndLanguageAttributeFileAccess.Delete, StringAndLanguageAttributeFileAccess.Read, StringAndLanguageAttributeFileAccess.ReadWrite, StringAndLanguageAttributeFileAccess.Write)] Enum valueToCompare, [Values(EnumStringRepresentationUsage.ToStringMethod, EnumStringRepresentationUsage.StringAttribute, EnumStringRepresentationUsage.LanguageStringAttribute)] EnumStringRepresentationUsage enumStringRepresentationUsage, [Values(EnumStringRepresentationUsage.ToStringMethod, EnumStringRepresentationUsage.StringAttribute, EnumStringRepresentationUsage.LanguageStringAttribute)] EnumStringRepresentationUsage enumStringRepresentationUsageOfValueToCompare) { var enumString = EnumString.Create <StringAndLanguageAttributeFileAccess>((StringAndLanguageAttributeFileAccess)value, enumStringRepresentationUsage); var other = EnumString.Create <StringAndLanguageAttributeFileAccess>((StringAndLanguageAttributeFileAccess)valueToCompare, enumStringRepresentationUsageOfValueToCompare); Assert.AreEqual(value.CompareTo(valueToCompare), enumString.CompareTo(other)); }
/// <summary>Gets the value of a specific property with respect to a null-based (row or column) index. /// </summary> /// <typeparam name="TEnum">The type of the output which is assumed to be a enumeration.</typeparam> /// <param name="rowIndex">The null-based index of the row.</param> /// <param name="columnIndex">The null-based index of the column.</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation.</param> /// <param name="value">The value (output).</param> /// <returns>A value indicating whether <paramref name="value"/> contains valid data.</returns> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represents a enumeration.</exception> public ExcelCellValueState TryGetValue <TEnum>(EnumStringRepresentationUsage enumStringRepresentationUsage, out TEnum value, int rowIndex, int columnIndex) where TEnum : struct, IComparable, IConvertible, IFormattable { if ((rowIndex < RowCount) && (rowIndex >= 0) && (columnIndex < ColumnCount) && (columnIndex >= 0)) { string valueDropDownListAsString = EnumString <TEnum> .GetValues(enumStringRepresentationUsage).AsExcelDropDownListString(); if (rowIndex < m_HeaderRange.RowLast - m_HeaderRange.RowFirst + 1) // query the header { m_HeaderRange.CreateDropdownList(rowIndex, columnIndex, valueDropDownListAsString); if (ExcelDataConverter.IsEmptyCell(m_HeaderData[rowIndex, columnIndex]) == true) { value = default(TEnum); m_GuidedExcelDataQuery.SetData(rowIndex, columnIndex, typeof(TEnum)); return(ExcelCellValueState.EmptyOrMissingExcelCell); } else if (ExcelDataConverter.TryGetCellValue <TEnum>(m_HeaderData[rowIndex, columnIndex], enumStringRepresentationUsage, out value) == true) { m_GuidedExcelDataQuery.SetData(rowIndex, columnIndex, value); return(ExcelCellValueState.ProperValue); } } else // query below the header { int adjRowIndex = rowIndex - (m_HeaderRange.RowLast - m_HeaderRange.RowFirst + 1); m_BelowHeaderRange.CreateDropdownList(adjRowIndex, columnIndex, valueDropDownListAsString); if (ExcelDataConverter.IsEmptyCell(m_BelowHeaderData[adjRowIndex, columnIndex]) == true) { value = default(TEnum); m_GuidedExcelDataQuery.SetData(rowIndex, columnIndex, typeof(TEnum)); return(ExcelCellValueState.EmptyOrMissingExcelCell); } else if (ExcelDataConverter.TryGetCellValue <TEnum>(m_BelowHeaderData[adjRowIndex, columnIndex], enumStringRepresentationUsage, out value) == true) { m_GuidedExcelDataQuery.SetData(rowIndex, columnIndex, value); return(ExcelCellValueState.ProperValue); } } } value = default(TEnum); return(ExcelCellValueState.NoValidValue); }
/// <summary>Gets the value of a specific property with respect to a null-based (row or column) index. /// </summary> /// <typeparam name="TEnum">The type of the output which is assumed to be a enumeration.</typeparam> /// <param name="rowIndex">The null-based index of the row.</param> /// <param name="columnIndex">The null-based index of the column.</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation.</param> /// <param name="value">The value (output).</param> /// <returns>A value indicating whether <paramref name="value"/> contains valid data.</returns> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represents a enumeration.</exception> public ExcelCellValueState TryGetValue <TEnum>(EnumStringRepresentationUsage enumStringRepresentationUsage, out TEnum value, int rowIndex, int columnIndex) where TEnum : struct, IComparable, IConvertible, IFormattable { if ((rowIndex < m_Data.Count) && (rowIndex >= 0) && (columnIndex < m_ColumnCount) && (columnIndex >= 0)) { object data = m_Data[rowIndex][columnIndex]; if ((data == null) || (data == Type.Missing) || (data is ExcelEmpty) || (data is ExcelMissing) || ((data is String) && (((string)data).Length == 0))) { value = default(TEnum); return(ExcelCellValueState.EmptyOrMissingExcelCell); } else if (data is TEnum) { value = (TEnum)data; return(ExcelCellValueState.ProperValue); } } value = default(TEnum); return(ExcelCellValueState.NoValidValue); }
/// <summary>Gets the value of a specific property with respect to a null-based (row or column) index. /// </summary> /// <typeparam name="TEnum">The type of the output which is assumed to be a enumeration.</typeparam> /// <param name="rowIndex">The null-based index of the row.</param> /// <param name="columnIndex">The null-based index of the column.</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation.</param> /// <param name="value">The value (output).</param> /// <returns>A value indicating whether <paramref name="value"/> contains valid data. /// </returns> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represents a enumeration.</exception> public ExcelCellValueState TryGetValue <TEnum>(EnumStringRepresentationUsage enumStringRepresentationUsage, out TEnum value, int rowIndex, int columnIndex) where TEnum : struct, IComparable, IConvertible, IFormattable { if ((rowIndex == 0) && (columnIndex == 0)) { if (ExcelDataConverter.IsEmptyCell(m_ExcelCellValue) == true) { value = default(TEnum); m_GuidedExcelDataQuery.SetData(0, 0, typeof(TEnum)); return(ExcelCellValueState.EmptyOrMissingExcelCell); } if (ExcelDataConverter.TryGetCellValue <TEnum>(m_ExcelCellValue, enumStringRepresentationUsage, out value) == true) { m_GuidedExcelDataQuery.SetData(0, 0, value); return(ExcelCellValueState.ProperValue); } } value = default(TEnum); return(ExcelCellValueState.NoValidValue); }
/// <summary>Gets the value of a specific property with respect to a null-based (row or column) index. /// </summary> /// <typeparam name="TEnum">The type of the output which is assumed to be a enumeration.</typeparam> /// <param name="rowIndex">The null-based index of the row.</param> /// <param name="columnIndex">The null-based index of the column.</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation.</param> /// <param name="value">The value (output).</param> /// <returns>A value indicating whether <paramref name="value"/> contains valid data.</returns> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represents a enumeration.</exception> public ExcelCellValueState TryGetValue <TEnum>(EnumStringRepresentationUsage enumStringRepresentationUsage, out TEnum value, int rowIndex, int columnIndex) where TEnum : struct, IComparable, IConvertible, IFormattable { if (typeof(TEnum).IsEnum == false) { throw new ArgumentException("The type " + typeof(TEnum).ToString() + " does not represents a enumeration.", "TEnum"); } if (rowIndex == 0) { ExcelCellValueState state = ExcelCellValueState.NoValidValue; if (columnIndex == 0) { state = TryGetValue <TEnum>(m_PropertyName, out value, enumStringRepresentationUsage); } else if (columnIndex == 1) { state = TryGetValue <TEnum>(m_PropertyValue, out value, enumStringRepresentationUsage); } else { value = default(TEnum); state = ExcelCellValueState.NoValidValue; } if (state == ExcelCellValueState.ProperValue) { m_GuidedExcelDataQuery.SetData(rowIndex, columnIndex, value); } else if (state == ExcelCellValueState.EmptyOrMissingExcelCell) { m_GuidedExcelDataQuery.SetData(rowIndex, columnIndex, typeof(TEnum)); } return(state); } value = default(TEnum); return(ExcelCellValueState.NoValidValue); }
/// <summary>Sets an optional property value, i.e. if a specific property value is available the value will be used /// to change the state of a specific object, otherwise the standard value will be use. /// </summary> /// <typeparam name="TEnum">The type of the value which is assumed to be a enumeration.</typeparam> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="propertyName">The name of the property.</param> /// <param name="trySetPropertyValue">A delegate which is used to set the value of the property with respect to a specific object, if the property value is available.</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation of the enumeration <typeparamref name="TEnum"/>.</param> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> /// <remarks>Use this method for properties which have some standard value if the user does not /// enter a specific value, for example the standard exit condition of an optimizer algorithm /// is the maximal number of iterations.</remarks> public static void SetOptionalPropertyValue <TEnum>(this IExcelDataQuery dataQuery, string propertyName, Func <TEnum, bool> trySetPropertyValue, EnumStringRepresentationUsage enumStringRepresentationUsage) where TEnum : struct, IComparable, IConvertible, IFormattable { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } TEnum propertyValue; ExcelPropertyValueState state = dataQuery.TryGetPropertyValue <TEnum>(propertyName, out propertyValue, enumStringRepresentationUsage); if (state == ExcelPropertyValueState.ProperProperty) { if (trySetPropertyValue(propertyValue) == false) { throw new ArgumentException("Invalid data for property '" + propertyName + "'" + GetFormatedDataQueryName(dataQuery) + "."); } } else if (state == ExcelPropertyValueState.NoValidValue) { throw new ArgumentException("Invalid data for property '" + propertyName + "'" + GetFormatedDataQueryName(dataQuery) + "."); } }
/// <summary>Gets the [optional] value of a specific Excel cell. /// </summary> /// <typeparam name="TEnum">The type of the output which is assumed to be a enumeration.</typeparam> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="value">On input this is a standard value; on exist this argument will be changed if and only if <paramref name="dataQuery"/> contains valid data at the desired position.</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation.</param> /// <param name="rowIndex">The null-based index of the row.</param> /// <param name="columnIndex">The null-based index of the column.</param> /// <returns>A value indicating whether <paramref name="value"/> has been changed and set to some user input.</returns> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represents an enumeration or if the user input at the desired position can not converted to <typeparamref name="TEnum"/>.</exception> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> public static bool TryGetOptionalValue <TEnum>(this IExcelDataQuery dataQuery, ref TEnum value, EnumStringRepresentationUsage enumStringRepresentationUsage, int rowIndex = 0, int columnIndex = 0) where TEnum : struct, IComparable, IConvertible, IFormattable { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } TEnum tempValue; ExcelCellValueState state = dataQuery.TryGetValue <TEnum>(enumStringRepresentationUsage, out tempValue, rowIndex, columnIndex); if (state == ExcelCellValueState.NoValidValue) { throw new ArgumentException("No valid input '" + dataQuery.ToString(rowIndex, columnIndex) + "'" + GetFormatedDataQueryName(dataQuery) + "."); } else if (state == ExcelCellValueState.ProperValue) { value = tempValue; return(true); } return(false); }
/// <summary>Gets the value of a specific Excel cell. /// </summary> /// <typeparam name="TEnum">The type of the output which is assumed to be a enumeration.</typeparam> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="value">The value (output).</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation.</param> /// <param name="rowIndex">The null-based index of the row.</param> /// <param name="columnIndex">The null-based index of the column.</param> /// <returns>A value indicating whether <paramref name="value"/> contains valid data.</returns> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represents a enumeration.</exception> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> public static ExcelCellValueState TryGetValue <TEnum>(this IExcelDataQuery dataQuery, out TEnum value, EnumStringRepresentationUsage enumStringRepresentationUsage, int rowIndex = 0, int columnIndex = 0) where TEnum : struct, IComparable, IConvertible, IFormattable { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } return(dataQuery.TryGetValue <TEnum>(enumStringRepresentationUsage, out value, rowIndex, columnIndex)); }
/// <summary>Gets the value of a specific [optional] property. /// </summary> /// <typeparam name="TEnum">The type of the value which is assumed to be a enumeration.</typeparam> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="propertyName">The name of the property to search.</param> /// <param name="value">On input this is a standard value of the property; on exit this argument will be changed if and only if a property with /// name <paramref name="propertyName"/> exists and contains valid data.</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation of the enumeration <typeparamref name="TEnum"/>.</param> /// <param name="propertyValueColumnIndex">The null-based index of the column which contains the value, the second column is standard.</param> /// <returns>A value indicating whether <paramref name="value"/> has been changed with user input.</returns> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represents a specific enumeration or no <b>valid</b> user input is given, i.e. /// a property of name <paramref name="propertyName"/> exists but the value is non-empty and can not convert to <typeparamref name="TEnum"/>.</exception> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> /// <remarks>Use this method if some standard values are available and the user has the option to change the standard setting.</remarks> public static bool TryGetOptionalPropertyValue <TEnum>(this IExcelDataQuery dataQuery, string propertyName, ref TEnum value, EnumStringRepresentationUsage enumStringRepresentationUsage, int propertyValueColumnIndex = 1) where TEnum : struct, IComparable, IConvertible, IFormattable { TEnum tempValue; ExcelPropertyValueState state = TryGetPropertyValue <TEnum>(dataQuery, propertyName, out tempValue, enumStringRepresentationUsage, propertyValueColumnIndex); if (state == ExcelPropertyValueState.NoValidValue) { throw new ArgumentException("No valid input for property '" + propertyName + "' found" + GetFormatedDataQueryName(dataQuery) + "."); } else if (state == ExcelPropertyValueState.ProperProperty) { value = tempValue; return(true); } return(false); }
/// <summary>Gets the value of a specific property. /// </summary> /// <typeparam name="TEnum">The type of the value which is assumed to be a enumeration.</typeparam> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="propertyName">The name of the property to search.</param> /// <param name="value">The value of the property (output).</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation of the enumeration <typeparamref name="TEnum"/>.</param> /// <param name="propertyValueColumnIndex">The null-based index of the column which contains the value, the second column is standard.</param> /// <returns>A value indicating whether <paramref name="value"/> contains valid data.</returns> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represents a specific enumeration.</exception> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> public static ExcelPropertyValueState TryGetPropertyValue <TEnum>(this IExcelDataQuery dataQuery, string propertyName, out TEnum value, EnumStringRepresentationUsage enumStringRepresentationUsage, int propertyValueColumnIndex = 1) where TEnum : struct, IComparable, IConvertible, IFormattable { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } int rowIndex; if (dataQuery.TryGetRowIndexOfPropertyName(propertyName, out rowIndex) == false) { value = default(TEnum); return(ExcelPropertyValueState.NoPropertyFound); } ExcelCellValueState state = dataQuery.TryGetValue <TEnum>(enumStringRepresentationUsage, out value, rowIndex, propertyValueColumnIndex); if (state == ExcelCellValueState.ProperValue) { return(ExcelPropertyValueState.ProperProperty); } else if (state == ExcelCellValueState.EmptyOrMissingExcelCell) { return(ExcelPropertyValueState.ValueIsEmptyExcelCell); } else { return(ExcelPropertyValueState.NoValidValue); } }
/// <summary>Gets the value of a specific required property. /// </summary> /// <typeparam name="TEnum">The type of the value which is assumed to be a enumeration.</typeparam> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="propertyName">The name of the property to search.</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation of the enumeration <typeparamref name="TEnum"/>.</param> /// <param name="propertyValueColumnIndex">The null-based index of the column which contains the value, the second column is standard.</param> /// <returns>The value of the property.</returns> /// <exception cref="ArgumentException">Thrown, if no property or valid value is given by the user or if /// <typeparamref name="TEnum"/> does not represents a specific enumeration.</exception> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> public static TEnum GetRequiredPropertyValue <TEnum>(this IExcelDataQuery dataQuery, string propertyName, EnumStringRepresentationUsage enumStringRepresentationUsage, int propertyValueColumnIndex = 1) where TEnum : struct, IComparable, IConvertible, IFormattable { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } TEnum value; int rowIndex; if (dataQuery.TryGetRowIndexOfPropertyName(propertyName, out rowIndex) == false) { throw new ArgumentException("No property with name '" + propertyName + "' found" + GetFormatedDataQueryName(dataQuery) + "."); } if (dataQuery.TryGetValue <TEnum>(enumStringRepresentationUsage, out value, rowIndex, propertyValueColumnIndex) == ExcelCellValueState.ProperValue) { return(value); } throw new ArgumentException("No valid input for property '" + propertyName + "' found" + GetFormatedDataQueryName(dataQuery) + "."); }
public Enum Parse_StringAttributeFileAccessEnumName_ValidEnumValue(string stringRepresentation, EnumStringRepresentationUsage enumStringRepresentationUsage) { return(EnumString <StringAttributeFileAccess> .Parse(stringRepresentation, enumStringRepresentationUsage)); }
/// <summary>Gets the value of a specific Excel cell. /// </summary> /// <typeparam name="TEnum">The type of the output which is assumed to be a enumeration.</typeparam> /// <param name="inputCell">The input Excel cell.</param> /// <param name="value">The value of the Excel cell (output).</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation.</param> /// <returns>A value indicating whether <paramref name="value"/> contains valid data.</returns> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represents a enumeration.</exception> private ExcelCellValueState TryGetValue <TEnum>(object inputCell, out TEnum value, EnumStringRepresentationUsage enumStringRepresentationUsage) where TEnum : struct, IComparable, IConvertible, IFormattable { object excelCellValue; if (inputCell == null) { value = default(TEnum); return(ExcelCellValueState.NoValidValue); } else if (inputCell is ExcelReference) { ExcelReference excelRange = (ExcelReference)inputCell; string valueDropDownListAsString = EnumString <TEnum> .GetValues(enumStringRepresentationUsage).AsExcelDropDownListString(); excelRange.CreateDropdownList(0, 0, valueDropDownListAsString); excelCellValue = excelRange.GetValue(); } else { excelCellValue = inputCell; } if (ExcelDataConverter.IsEmptyCell(excelCellValue) == true) { value = default(TEnum); return(ExcelCellValueState.EmptyOrMissingExcelCell); } return((ExcelDataConverter.TryGetCellValue <TEnum>(excelCellValue, enumStringRepresentationUsage, out value) == true) ? ExcelCellValueState.ProperValue : ExcelCellValueState.NoValidValue); }
public Enum Parse_StringAndLanguageAttributeFileAccessEnumName_ValidEnumValue(string stringRepresentation, EnumStringRepresentationUsage enumStringRepresentationUsage) { return(EnumString.Parse(typeof(StringAndLanguageAttributeFileAccess), stringRepresentation, enumStringRepresentationUsage)); }
/// <summary>Converts the string representation of an enumeration item to its enumeration item equivalent.</summary> /// <param name="stringRepresentation">The <see cref="System.String"/> representation to search for.</param> /// <param name="value">The element of the enumeration with respect to <paramref name="stringRepresentation"/> (output).</param> /// <param name="enumStringRepresentationUsage">The method how to compute the string representation of the items of the /// enumeration represented by <typeparamref name="TEnum"/>.</param> /// <returns>A value indicating whether <paramref name="value"/> contains valid data.</returns> /// <remarks>White spaces etc. will be ignored.</remarks> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represent an enumeration.</exception> public static bool TryParse(string stringRepresentation, out TEnum value, EnumStringRepresentationUsage enumStringRepresentationUsage = EnumStringRepresentationUsage.LanguageStringAttribute) { if (typeof(TEnum).IsEnum == false) { throw new ArgumentException(String.Format(ExceptionMessages.ArgumentIsInvalid, "TEnum: " + typeof(TEnum).ToString())); } if ((stringRepresentation == null) || (stringRepresentation.Length == 0)) { value = default; return(false); } string stringRepresentationID = IdentifierString.GetIDString(stringRepresentation, false); if (enumStringRepresentationUsage == EnumStringRepresentationUsage.ToStringMethod) { return(Enum.TryParse <TEnum>(stringRepresentationID.Replace(EnumString.FlagsEnumSeparatorChar, EnumString.dotNetFlagsEnumSeparatorChar), true, out value)); } ResourceManager resourceManager = null; Type enumerationType = typeof(TEnum); if (enumStringRepresentationUsage == EnumStringRepresentationUsage.LanguageStringAttribute) { if (Attribute.GetCustomAttribute(enumerationType, typeof(LanguageResourceAttribute)) is LanguageResourceAttribute languageResourceFile) { resourceManager = new ResourceManager(languageResourceFile.FullResourceName, enumerationType.Assembly); } } /* if the enumeration contains the 'Flags' attribute, we assume that the input is given with respect to some * comma separated (see 'FlagsEnumSeparatorChar') list and the return value will be the bitwise OR relation of the corresponding elements. */ if (enumerationType.GetCustomAttributes(typeof(FlagsAttribute), false).Length == 0) // no [Flags] attribute available { foreach (TEnum enumValue in Enum.GetValues(enumerationType)) // linear is no problem because the number of elements is in general very small { if (GetFormatString(enumValue, resourceManager) == stringRepresentationID) { value = enumValue; return(true); } } // it is not a Flag, but perhaps a bitwise OR combination (which is unusual for a non-flag enumeration) } /* split the strings into a list of strings, where the 'FlagsEnumSeparatorChar' is take into * account, transform each of the sub-strings to some ID-representation, compare to the elements * of the given list and build a new string, which will be used for the Parse-Method of System.Enum. * Here, we can not apply '|=' to the matched elements, that's the reason to do it this way: */ StringBuilder bString = new StringBuilder(); foreach (string subString in stringRepresentationID.Split(EnumString.FlagsEnumSeparatorChar)) { foreach (TEnum enumValue in Enum.GetValues(enumerationType)) // linear is no problem because the number of elements is in general very small { if (GetFormatString(enumValue, resourceManager) == subString) { if (bString.Length > 0) { bString.Append(EnumString.dotNetFlagsEnumSeparatorChar); } bString.Append(enumValue.ToString()); // transform to the internal string representation } } } return(Enum.TryParse <TEnum>(bString.ToString(), true, out value)); }
public Enum Parse_FileAccessEnumName_ValidEnumValue(string stringRepresentation, EnumStringRepresentationUsage enumStringRepresentationUsage) { return(EnumString.Parse(typeof(FileAccess), stringRepresentation, enumStringRepresentationUsage)); }
public Enum Parse_PartialAttributeWeekdaysEnumName_ValidEnumValue(string stringRepresentation, EnumStringRepresentationUsage enumStringRepresentationUsage) { return(EnumString <PartialAttributeWeekdays> .Parse(stringRepresentation, enumStringRepresentationUsage)); }
/// <summary>Gets a <see cref="System.String"/> representation that takes into account <see cref="LanguageResourceAttribute"/>, /// <see cref="LanguageStringAttribute"/> or <see cref="StringAttribute"/>; if available. /// </summary> /// <param name="value">The value.</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation.</param> /// <returns>A <see cref="System.String"/> representation of <paramref name="value"/> with respect to <paramref name="enumStringRepresentationUsage"/>, i.e. /// <see cref="LanguageResourceAttribute"/>, <see cref="LanguageStringAttribute"/>, <see cref="StringAttribute"/> /// or the <c>ToString()</c> method is used to generate the string representation.</returns> public static string ToFormatString(this Enum value, EnumStringRepresentationUsage enumStringRepresentationUsage = EnumStringRepresentationUsage.LanguageStringAttribute) { if (enumStringRepresentationUsage == EnumStringRepresentationUsage.ToStringMethod) { return(value.ToString()); } else if (enumStringRepresentationUsage == EnumStringRepresentationUsage.StringAttribute) { if (Enum.IsDefined(value.GetType(), value) == true) { StringAttribute stringAttribute = EnumAttribute.Create <StringAttribute>(value); if (stringAttribute != null) { return(stringAttribute.StringRepresentation); } return(value.ToString()); } else // is a flag, i.e. a|b|c etc. which is not a member of the enumeration, but perhaps 'b|c' is in the enumeration! { StringBuilder strBuilder = new StringBuilder(); foreach (Enum enumValue in GetEnumComponents(value)) { if (strBuilder.Length > 0) { strBuilder.Append(EnumString.FlagsEnumSeparatorChar); } StringAttribute stringAttribute = EnumAttribute.Create <StringAttribute>(enumValue); if (stringAttribute != null) { strBuilder.Append(stringAttribute.StringRepresentation); } else { strBuilder.Append(enumValue.ToString()); } } return(strBuilder.ToString()); } } // otherwise take into account a language depended string representation, if available: Type enumType = value.GetType(); LanguageResourceAttribute languageResourceFile = Attribute.GetCustomAttribute(enumType, typeof(LanguageResourceAttribute)) as LanguageResourceAttribute; if (Enum.IsDefined(value.GetType(), value) == true) { if (languageResourceFile != null) { LanguageStringAttribute languageStringAttribute = EnumAttribute.Create <LanguageStringAttribute>(value); if (languageStringAttribute != null) { ResourceManager resourceManager = new ResourceManager(languageResourceFile.FullResourceName, enumType.Assembly); return(resourceManager.GetString(languageStringAttribute.ResourcePropertyName, Thread.CurrentThread.CurrentUICulture)); } } StringAttribute stringAttribute = EnumAttribute.Create <StringAttribute>(value); if (stringAttribute != null) { return(stringAttribute.StringRepresentation); } } else // the enumeration is some [Flag] and only the parts contains a attribute but not the bitwise combination; we use ',' as separator { StringBuilder strBuilder = new StringBuilder(); foreach (Enum enumValue in GetEnumComponents(value)) { if (strBuilder.Length > 0) { strBuilder.Append(EnumString.FlagsEnumSeparatorChar); } if (languageResourceFile != null) { LanguageStringAttribute languageStringAttribute = EnumAttribute.Create <LanguageStringAttribute>(enumValue); if (languageStringAttribute != null) { ResourceManager resourceManager = new ResourceManager(languageResourceFile.FullResourceName, enumType.Assembly); strBuilder.Append(resourceManager.GetString(languageStringAttribute.ResourcePropertyName, Thread.CurrentThread.CurrentUICulture)); continue; // to to next enumValue } } StringAttribute stringAttribute = EnumAttribute.Create <StringAttribute>(enumValue); if (stringAttribute != null) { strBuilder.Append(stringAttribute.StringRepresentation); } else { strBuilder.Append(enumValue.ToString()); } } return(strBuilder.ToString()); } return(value.ToString()); }