如何最好地处理system.reflection.propertyInfo NULLReferenceExceptions

时间:2014-03-05 07:30:18

标签: c# c#-4.0 system.reflection

  public DataTable GenericListToDataTable(object list)
    {
        DataTable dt = null;
        Type listType = list.GetType();
        if (listType.IsGenericType)
        {
          Type elementType = listType.GetGenericArguments()[0];
             dt = new DataTable(elementType.Name + "List");


            MemberInfo[] miarray = elementType.GetMembers(BindingFlags.Public | BindingFlags.Instance);
            foreach (MemberInfo mi in miarray)
            {
                if (mi.MemberType == MemberTypes.Property)
                {
                    PropertyInfo pi = mi as PropertyInfo;
                    dt.Columns.Add(pi.Name, pi.PropertyType);

                }
                else if (mi.MemberType == MemberTypes.Field)
                {
                    FieldInfo fi = mi as FieldInfo;
                    dt.Columns.Add(fi.Name, fi.FieldType);
                }
            }

            //populate the table 
            IList il = list as IList;
            foreach (object record in il)
            {
                int i = 0;
                object[] fieldValues = new object[dt.Columns.Count];
                foreach (DataColumn c in dt.Columns)
                {
                    MemberInfo mi = elementType.GetMember(c.ColumnName)[0];
                    if (mi.MemberType == MemberTypes.Property)
                    {
                        PropertyInfo pi = mi as PropertyInfo;
                        fieldValues[i] = pi.GetValue(record, null);
                    }
                    else if (mi.MemberType == MemberTypes.Field)
                    {
                        FieldInfo fi = mi as FieldInfo;
                        fieldValues[i] = fi.GetValue(record);
                    }
                    i++;
                }
                dt.Rows.Add(fieldValues);
            }
        }
        return dt;
    }

在上面的代码中,我试图将通用List转换为DataTable但我在如何最好地处理dt.Columns.Add(pi.Name, pi.PropertyType);

的NullRefrenceException方面遇到了麻烦
dt.Columns.Add(fi.Name, fi.FieldType);

我试过了

if (pi != null) dt.Columns.Add(pi.Name, pi.PropertyType);

但仍然可以获得以下内容

System.NotSupportedException:DataSet不支持System.Nullable<>。

1 个答案:

答案 0 :(得分:0)

编辑:当然,您需要像以前一样跳过空值!

答案可能不是您想听到的:DataColumn根本不支持Nullable类型。检查代码:

    // System.Data.DataColumn
    public DataColumn(string columnName, Type dataType, string expr, MappingType type)
    {
        GC.SuppressFinalize(this);
        Bid.Trace("<ds.DataColumn.DataColumn|API> %d#, columnName='%ls', expr='%ls', type=%d{ds.MappingType}\n", this.ObjectID, columnName, expr, (int)type);
        if (dataType == null)
        {
            throw ExceptionBuilder.ArgumentNull("dataType");
        }
        StorageType storageType = DataStorage.GetStorageType(dataType);
        if (DataStorage.ImplementsINullableValue(storageType, dataType))
        {
            throw ExceptionBuilder.ColumnTypeNotSupported();
        }
        this._columnName = ((columnName == null) ? "" : columnName);
        SimpleType simpleType = SimpleType.CreateSimpleType(dataType);
        if (simpleType != null)
        {
            this.SimpleType = simpleType;
        }
        this.UpdateColumnType(dataType, storageType);
        if (expr != null && 0 < expr.Length)
        {
            this.Expression = expr;
        }
        this.columnMapping = type;
    }

ImplementsNullableValue看起来像这样:

    // System.Data.Common.DataStorage
    internal static bool ImplementsINullableValue(StorageType typeCode, Type dataType)
    {
        return typeCode == StorageType.Empty && dataType.IsGenericType && dataType.GetGenericTypeDefinition() == typeof(Nullable<>);
    }

所以我担心你有两个可能性:

  • 跳过Nullable类型
  • 为你的可空类型创建一个包装类(取决于这是否可行,例如很多bool?会有一些工作)