如何使用 属性 Name of Class 和对象索引获取 IList 的索引,其中 属性 names of both object and class Match?

How to get the index for IList using Property Name of Class and object Index where Property names of both object and class Match?

我不确定我是否解释正确,或者问题是否足够清楚,但这就是我想做的。

我创建了一个名为 index 的对象,它存储 IList 中元素的索引。这是我的对象索引。

var index = new
                {
                    PhoneReportID = 1,
                    CSQID = i.NextIndex(),
                    CallsPresented = i.NextIndex(),
                    AvgQueueTime = i.NextIndex(),
                    CallsHandled = i.NextIndex(),
                    AvgAnswerSpeed = i.NextIndex(),
                    AvgHandleTime = i.NextIndex(),
                    CallsAbandoned = i.NextIndex(),
                    AvgAbandonTime = i.NextIndex(),
                    AvgCallsAbandoned = i.NextIndex(),
                    CallsDequeued = i.NextIndex(),
                    AvgDequeueTime = i.NextIndex(),
                    CallsHandledByOther = i.NextIndex(),
                    MaxQueueTime = z.NextIndex(),
                    MaxHandleTime = z.NextIndex(),
                    MaxAbandonTime = z.NextIndex(),
                    MaxCallsAbandoned = z.NextIndex(),
                    MaxDequeueTime = z.NextIndex(),

                };

这是我的通用方法,它从通用类型获取 属性 并根据索引中的元素位置设置值。

 private IEnumerable<T> Parse(IList<string[]> rows, object index)
        {
            Type indexType = index.GetType();
            IList<PropertyInfo> indexProperties = indexType.GetProperties();
            int k = 0;

            var classType = typeof(T);
            IList<PropertyInfo> classProperties = classType.GetProperties();

            foreach (var property in classProperties)//.Select(x => x.ToString()).Select(y => y.Trim()).ToList())
            {
                var propGetter = property.GetGetMethod();
                var propSetter = indexType.GetProperty(property.Name).GetSetMethod();
                var valueToSet = rows[int.TryParse(index[indexType.GetProperty(property.Name)],out k)];


            }
            throw new Exception();
        }

我这样做的原因有很多,但我这样做的主要原因之一是学习 C# 中的反射。

因此,rows 是一个列表,它是通过读取具有一定分隔符的文件而获得的。由于列表中的每个值都对应于 class 的某些属性,我创建了一个名为 index 的对象,其名称与 class 的 属性 名称相同,并为其分配了 class 的位置IList 中的值。现在,我想使用索引的 属性 名称检索行中的值,其中 returns class 的 属性 名称与 属性 对象索引的名称。我在上面所做的方法对于检索值是不正确的,但我写它是为了清楚我打算实现的目标。希望我足够清楚,有人可以回答这个问题。

private IEnumerable<T> Deserialize<T>(IList<string[]> rows, object index)
    where T : new()
{
    Type indexType = index.GetType();
    IList<PropertyInfo> indexProperties = indexType.GetProperties();

    var dtoType = typeof(T);
    IList<PropertyInfo> dtoProperties = dtoType.GetProperties();

    var setters = new List<Tuple<int,PropertyInfo>>();

    foreach (var dtoProperty in dtoProperties)
    {
        var indexProperty = indexType.GetProperty(dtoProperty.Name);
        var columnIndex = (int) indexProperty.GetValue(index);

        setters.Add(Tuple.Create(columnIndex, dtoProperty));
    }

    foreach (var row in rows)
    {
        var dto = new T();
        foreach (var pair in setters)
        {
            var colIndex = pair.Item1;
            var prop = pair.Item2;
            prop.SetValue(dto, row[colIndex]);
        }
        yield return dto;
    }
}
  • where T : new() 是一个通用约束,只允许具有无参数构造函数的类型。它允许您稍后使用 new T(),而编译器不会对您尖叫。
  • 我使用了 PropertyInfo.GetValue(object)PropertyInfo.SetValue(object, object) 而不是直接访问 get 和 set 方法,因为它更容易。