将IEnumerable转换为DataTable为其原始类型

时间:2019-06-28 23:37:38

标签: c# linq generics reflection

我有一个IEnumerable的Clients,并且Client类中的字段是:

    public string Name { get; set; }
    public int Age { get; set; }
    public Town Hometown { get; set; }

“家乡”类中的字段是:

    public string TownName { get; set; }
    public double Population { get; set; }
    public double Mortality{ get; set; }

目标是生成一个通用方法,该方法可以接收IEnumerable作为输入,并生成具有特定类的原始类型的DataTable。在此示例中,它将是以下几列:名称,年龄,城镇名称,人口和道德。

我尝试了以下代码:

    public DataTable TransformIEnumerableToDataTable<T>(IEnumerable<T> IEnumerableTable)
    {
        var props = typeof(T).GetProperties();

        foreach (PropertyInfo prop in props)
        {
            if (!prop.PropertyType.IsPrimitive || prop.PropertyType != typeof(Decimal) || prop.PropertyType != typeof(String))
            {
                // Here I can't move forward, I can't pass such argumento to the function typeof, so I don't know what do should be done
                var prop2 = typeof(prop.PropertyType);
            }
        }

        var dt = new DataTable();
        dt.Columns.AddRange(
          props.Select(p => new DataColumn(p.Name, Nullable.GetUnderlyingType(p.PropertyType) ?? p.PropertyType)).ToArray()
        );

        IEnumerableTable.ToList().ForEach(
          i => dt.Rows.Add(props.Select(p => p.GetValue(i, null)).ToArray())
        );

        return dt;
    }

不带foreach语句的输出将为我提供一个包含以下几列的DataTable:名称,年龄和家乡。但是,我希望输出具有原始镇的原始数据类型,而不是原始镇对象本身。

1 个答案:

答案 0 :(得分:0)

这是一组基于我的FlattenToExpandoFlattenToAnonymous方法的扩展方法,这些方法会将IEnumerable<T>展平为DataTable。请注意,集合类型不会展平,而类只会展平一层。

有了这些,您可以将方法实现为

public DataTable TransformIEnumerableToDataTable<T>(IEnumerable<T> IEnumerableTable) => IEnumerableTable.FlattenToDataTable();

扩展方法:

public static class DataTableExt {
    // convert a class containing members that are composite into a flattened DataTable
    // only flatten one level deep; do not flatten collections
    public static DataTable FlattenToDataTable<T>(this IEnumerable<T> src) {
        var oldMITs = typeof(T).GetMITs();
        var ans = new DataTable(nameof(T));

        foreach (var mit in oldMITs)
            if (mit.IsSimpleOrEnumerable)
                ans.Columns.Add(new DataColumn(mit.mi.Name, mit.mt));

        foreach (var mit in oldMITs)
            if (!mit.IsSimpleOrEnumerable)
                foreach (var subMI in mit.subMIs) {
                    // find unique name
                    var possibleName = subMI.Name;
                    var namect = 1;
                    while (ans.Columns.Contains(possibleName)) {
                        possibleName = $"{subMI.Name}{namect}";
                        ++namect;
                    }
                    ans.Columns.Add(new DataColumn(possibleName, subMI.GetMemberType()));
                }

        foreach (var srcObj in src) {
            var dr = ans.NewRow();
            srcObj.CopyFlattenedToDataRow(oldMITs, dr);
            ans.Rows.Add(dr);
        }

        return ans;
    }

    class MemberInfoType {
        public MemberInfo mi;
        public Type mt;
        public bool IsSimpleOrEnumerable;
        public List<MemberInfo> subMIs;
    }

    static List<MemberInfoType> GetMITs(this Type t) =>
        t.GetPropertiesOrFields().Select(mi => new { mi = mi, mt = mi.GetMemberType() })
        .Select(mit => new { mit, IsSimpleOrEnumerable = mit.mt.IsSimple() || mit.mt.IsIEnumerable() })
        .Select(mitb => new MemberInfoType { mi = mitb.mit.mi, mt = mitb.mit.mt, IsSimpleOrEnumerable = mitb.IsSimpleOrEnumerable, subMIs = mitb.IsSimpleOrEnumerable ? null : mitb.mit.mt.GetPropertiesOrFields() })
        .ToList();

    static void CopyFlattenedToDataRow<T>(this T srcObject, List<MemberInfoType> oldMITs, DataRow ansRow) {
        var colIdx = 0;

        foreach (var mit in oldMITs)
            if (mit.IsSimpleOrEnumerable)
                ansRow[colIdx++] = mit.mi.GetValue(srcObject);

        foreach (var mit in oldMITs)
            if (!mit.IsSimpleOrEnumerable) {
                var subObj = mit.mi.GetValue(srcObject);
                foreach (var subMI in mit.subMIs)
                    ansRow[colIdx++] = subMI.GetValue(subObj);
            }
    }

    // ***
    // *** Type Extensions
    // ***
    public static List<MemberInfo> GetPropertiesOrFields(this Type t, BindingFlags bf = BindingFlags.Public | BindingFlags.Instance) =>
        t.GetMembers(bf).Where(mi => mi.MemberType == MemberTypes.Field || mi.MemberType == MemberTypes.Property).ToList();

    // ***
    // *** MemberInfo Extensions
    // ***
    public static Type GetMemberType(this MemberInfo member) {
        switch (member) {
            case FieldInfo mfi:
                return mfi.FieldType;
            case PropertyInfo mpi:
                return mpi.PropertyType;
            case EventInfo mei:
                return mei.EventHandlerType;
            default:
                throw new ArgumentException("MemberInfo must be if type FieldInfo, PropertyInfo or EventInfo", nameof(member));
        }
    }

    public static object GetValue(this MemberInfo member, object srcObject) {
        switch (member) {
            case FieldInfo mfi:
                return mfi.GetValue(srcObject);
            case PropertyInfo mpi:
                return mpi.GetValue(srcObject);
            case MethodInfo mi:
                return mi.Invoke(srcObject, null);
            default:
                throw new ArgumentException("MemberInfo must be of type FieldInfo, PropertyInfo or MethodInfo", nameof(member));
        }
    }
    public static T GetValue<T>(this MemberInfo member, object srcObject) => (T)member.GetValue(srcObject);

    public static void SetValue(this MemberInfo member, object destObject, object value) {
        switch (member) {
            case FieldInfo mfi:
                mfi.SetValue(destObject, value);
                break;
            case PropertyInfo mpi:
                mpi.SetValue(destObject, value);
                break;
            case MethodInfo mi:
                mi.Invoke(destObject, new object[] { value });
                break;
            default:
                throw new ArgumentException("MemberInfo must be of type FieldInfo, PropertyInfo or MethodInfo", nameof(member));
        }
    }
    public static void SetValue<T>(this MemberInfo member, object destObject, T value) => member.SetValue(destObject, (object)value);

    // ***
    // *** Type Extensions
    // ***
    public static bool IsNullableType(this Type aType) =>
    // instantiated generic type only                
        aType.IsGenericType &&
        !aType.IsGenericTypeDefinition &&
        Object.ReferenceEquals(aType.GetGenericTypeDefinition(), typeof(Nullable<>));

    // Stack Overflow
    public static bool IsSimple(this Type type) =>
        type.IsNullableType() ? type.GetGenericArguments()[0].IsSimple()
                              : type.IsPrimitive ||
                                type.IsEnum ||
                                type.Equals(typeof(string)) ||
                                type.Equals(typeof(decimal)) ||
                                type.Equals(typeof(TimeSpan)) ||
                                type.Equals(typeof(DateTime));
    public static bool IsIEnumerable(this Type type) => typeof(IEnumerable).IsAssignableFrom(type);
}