0
NetworkElementCountersFactory factory=new NetworkElementCountersFactory();
List<NetworkElementCounters> neCountersList= new List<NetworkElementCounters>();
NetworkElementCounters neCounters;
while (reader.Read())
{
    i = 4;
    neCounters = factory.getInstance(tableName, reader.GetInt32(0), reader.GetDateTime(1), reader.GetDateTime(2), reader.GetInt32(3));
    foreach (var v in neCounters.Fields)
    {
        v.GetType().GetProperty("CounterValue").SetValue(neCounters.GetType(), reader.GetValue(i), null);
        i++;
    }
    neCountersList.Add(neCounters);
} 

我在这里收到异常:

v.GetType().GetProperty("CounterValue").SetValue(neCounters.GetType(), reader.GetValue(i), null);
4

1 回答 1

3

这看起来非常错误:

.SetValue(neCounters.GetType(), {whatever}, null);

这意味着您正在尝试在Type实例上分配它。您应该在此处传递目标对象,或者null如果它是一个static属性。看起来应该是这样的:

.SetValue(neCounters, {whatever}, null);

但这样会更容易使用:

neCounters.CounterValue = ...
// v.CounterValue = ... // <=== might be this instead - confusing context

dynamic如果这里有一些复杂性,也许可以通过:

dynamic obj = neCounters;
// dynamic obj = v; // <=== might be this instead - confusing context
obj.CounterValue = reader.GetValue(i);
于 2012-05-02T07:27:21.030 回答