使用反射忽略具有自定义属性集的属性

时间:2016-11-14 15:44:24

标签: c# reflection custom-attributes

我编写了一个泛型方法,用于从Datatable生成泛型类型的集合。我已经寻找了不同的实现,但是在处理大量属性和大量记录时,大多数都表现得非常糟糕。到目前为止,这个表现相当不错。

我尝试通过在属性上添加自定义属性(DataField)来改进方法,这样我就可以将它包含在属性上,我可以跳过与列匹配,或指定自定义名称与数据表的列名匹配的属性。

我查看了代码,现在看起来很乱,我真的不为此感到骄傲,我希望有更好的实现。谁能给我一些提示?非常感激。

试图包含评论,不确定它有多大帮助。谢谢,这是代码:

private static void SetItemFromRow<T>(T item, DataRow row) where T : new()
    {
        // Get all properties with attributes.
        PropertyInfo[] propWithAttributes = item.GetType().GetProperties().Where(x => Attribute.IsDefined
          (x, typeof(DataField))).ToArray();

        foreach (DataColumn col in row.Table.Columns)
        {
            // Find property that matches the column name.
            PropertyInfo p = item.GetType().GetProperty(col.ColumnName);
            bool ignoreProperty = false;

            if (p != null)
            {
                // If no attribute exists set the property value. Break out from the loop to go to the next column (Property).
                if (!propWithAttributes.Contains(p))
                {
                    if (row[col] != DBNull.Value)
                    {
                        p.SetValue(item, row[col], null);
                        continue;
                    }
                }

                // If the property has a custom attribute then check if its ignore property is true. If so we break out from the loop and go to the next column (Property). 
                var attrs = p.GetCustomAttributes(typeof(DataField), false).ToArray() as DataField[]; ;

                if (attrs != null)
                    foreach (var attr in attrs)
                    {
                        if (attr.Ignore)
                            ignoreProperty = true;
                    }

                if (ignoreProperty) continue;
            }

            SetPropertyWithCustomName(item, propWithAttributes, row, col);    
        }
    }

现在我们在具有匹配列名的对象上设置了所有属性,我们也跳过了我们想要忽略的所有属性。最后一步是设置具有已定义名称的DataField属性的属性。

            private static void SetPropertyWithCustomName<T>(T item, PropertyInfo[] propWithAttributes,  DataRow row,  DataColumn col)
        where T : new()
    {

        foreach (var prop in propWithAttributes)
        {
            // Get the attributes for the property.
            var attrs = prop.GetCustomAttributes(typeof(DataField), false).ToArray() as DataField[];
            bool match = false;

            if (attrs != null)
            {
                foreach (var attr in attrs)
                {
                    // Check if the column name matches the custom name on the property.
                    if (col.ColumnName == attr.Name)
                    {
                        var p = item.GetType().GetProperty(prop.Name);
                        if (row[col] != DBNull.Value)
                        {
                            p.SetValue(item, row[col], null);
                            match = true;
                            break;
                        }
                    }
                }

            if (match) break;

            }
        }
    }

1 个答案:

答案 0 :(得分:0)

这是一个更易读的代码版本(如果我理解正确的意图):

private static readonly Dictionary<Type, DataFieldProperty[]> _propsCache = new Dictionary<Type, DataFieldProperty[]>();
private static DataFieldProperty[] GetProperties(Type type) {
    lock (_propsCache) {
        if (!_propsCache.ContainsKey(type)) {
            var result = new List<DataFieldProperty>();
            foreach (var prop in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)) {
                var attr = prop.GetCustomAttribute<DataField>();
                result.Add(new DataFieldProperty {
                    Name = attr?.Name ?? prop.Name,
                    Ignore = attr?.Ignore ?? false,
                    Property = prop
                });
           }
           _propsCache.Add(type, result.ToArray());
        }
        return _propsCache[type];
    }
}

private class DataFieldProperty {
    public string Name { get; set; }
    public PropertyInfo Property { get; set; }
    public bool Ignore { get; set; }
}

private static void SetItemFromRow<T>(T item, DataRow row) where T : new() {
    // Get all properties with attributes.
    var props = GetProperties(item.GetType());
    foreach (DataColumn col in row.Table.Columns) {
        // Find property that matches the column name.
        var p = props.FirstOrDefault(c => c.Name == col.ColumnName && !c.Ignore);
        if (p != null) {
            if (row[col] != DBNull.Value) {
                p.Property.SetValue(item, row[col], null);
            }
        }
    }
}

请注意,我实际上没有运行它(但已验证它已编译)。