Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support convert enum type value in InfluxData.Net.InfluxDb.Helpers.SerieExtensions.As<T> #91

Open
fissssssh opened this issue Feb 24, 2022 · 0 comments

Comments

@fissssssh
Copy link

before

/// <summary>
/// Converts an ubiquitous enumeration of series to a strongly typed enumeration by matching property names.
/// </summary>
/// <typeparam name="T">Type to convert the enumeration of series to</typeparam>
/// <param name="series">Series to convert</param>
/// <returns>Strongly typed enumeration representing the series</returns>
public static IEnumerable<T> As<T>(this IEnumerable<Serie> series) where T : new()
{
    if (series == null)
        yield return default(T);

    Type type = typeof(T);

    foreach (var serie in series)
    {
        var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList();

        var matchedProperties = serie.Columns
            .Select(columnName => properties.FirstOrDefault(
                property => String.Compare(property.Name, columnName, StringComparison.InvariantCultureIgnoreCase) == 0))
            .ToList();

        foreach (var value in serie.Values)
        {
            var instance = new T();

            for (var columnIndex = 0; columnIndex < serie.Columns.Count(); columnIndex++)
            {
                var prop = matchedProperties[columnIndex];

                if (prop == null)
                    continue;

                Type propType = prop.PropertyType;

                var convertedValue = Convert.ChangeType(value[columnIndex], prop.PropertyType);

                prop.SetValue(instance, convertedValue);
            }

            yield return instance;
        }
    }
}

after

/// <summary>
/// Converts an ubiquitous enumeration of series to a strongly typed enumeration by matching property names.
/// </summary>
/// <typeparam name="T">Type to convert the enumeration of series to</typeparam>
/// <param name="series">Series to convert</param>
/// <returns>Strongly typed enumeration representing the series</returns>
public static IEnumerable<T> As<T>(this IEnumerable<Serie> series) where T : new()
{
    if (series == null)
        yield return default(T);

    Type type = typeof(T);

    foreach (var serie in series)
    {
        var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList();

        var matchedProperties = serie.Columns
            .Select(columnName => properties.FirstOrDefault(
                property => String.Compare(property.Name, columnName, StringComparison.InvariantCultureIgnoreCase) == 0))
            .ToList();

        foreach (var value in serie.Values)
        {
            var instance = new T();

            for (var columnIndex = 0; columnIndex < serie.Columns.Count(); columnIndex++)
            {
                var prop = matchedProperties[columnIndex];

                if (prop == null)
                    continue;

                Type propType = prop.PropertyType;

                object convertedValue;

                if (propType.IsEnum)
                {
                    convertedValue = Enum.ToObject(propType, value[columnIndex]);
                }
                else
                {
                    convertedValue = Convert.ChangeType(value[columnIndex], prop.PropertyType);
                }

                prop.SetValue(instance, convertedValue);
            }

            yield return instance;
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant