3

我从一个我无法控制的库中得到一个接口作为返回:

public interface IA : IB { String A { get;} }
public interface IB { String B { get;} }

现在,当我尝试运行此代码时,出现异常:

List<IA> list = library.Get();
IDataReader r = ObjectReader.Create(list, nameof(IA.A), nameof(IA.B));
while (r.Read())
{
    for (Int32 i = 0; i < r.FieldCount; i++)
    {
        //HERE ArgumentOutOfRangeException: Specified argument was out of the range of valid values.Parameter name: name
        Object o = r[i];
        Console.Write(o + ",");
    }
    Console.WriteLine();
}

似乎它没有找到该B属性,因为它是在 IB 中声明的。我通过进行测试并IA直接让 B 确认了这一点。

我发现了一个非常糟糕的解决方法,它涉及创建一个实现 IA 的新类:

public class Ab : IA
{
    public Ab(String a, String b)
    {A = a;B=b;}

    public String A { get;}
    public String B { get;}
}

然后将其转换List<IA>为:List<Ab> newList = l.Select(e => new Ab(e.A, e.B).ToList(),然后ObjectReader.Create(newList). B当我这样做时,ObjectReader 似乎找到了该属性。但这似乎非常浪费资源(和大量代码)来创建具有完全相同内容的中间对象。

是否有可能以不涉及创建新对象的另一种方式解决?

4

1 回答 1

2

我克隆了 FastMember 包的存储库,在TypeAccessor我更改了第 265 行的类中:

PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

至:

PropertyInfo[] props = GetPrpsInterfaces(type, BindingFlags.Public | BindingFlags.Instance).ToArray();

替换功能的实现:

static IEnumerable<PropertyInfo> GetPrpsInterfaces(Type type, BindingFlags flags)
{
      if (!type.IsInterface)
           return type.GetProperties(flags);

      return (new Type[] { type }).Concat(type.GetInterfaces()).SelectMany(i => i.GetProperties(flags));
}

我在这里找到: GetProperties() 返回接口继承层次结构的所有属性

这对我有用。

于 2018-07-20T13:46:04.060 回答