0

我正在做这样的事情:

foreach( KeyValuePair<int,SOME_CLASS> reader in _readerDict )
{
  var selectedReader = reader;

  var antenna1 = selectedReader.Value.Antenna1IsEnabled;
  var antenna2 = selectedReader.Value.Antenna2IsEnabled;
  var antenna3 = selectedReader.Value.Antenna3IsEnabled;
  var antenna4 = selectedReader.Value.Antenna4IsEnabled;
}

但话又说回来,我想做以下事情:

foreach( KeyValuePair<int,SOME_CLASS> reader in _readerDict )
{
  var selectedReader = reader;

  for( var idx = 1; idx <= 4; idx++ )
  {
    var antennaIsEnabled = string.Format( "selectedReader.Value.Antenna{0}IsEnabled", idx );

    // cast the value of antennaIsEnabled as a KeyValuePair<int,SOME_CLASS> variable datatype here
    ....
  }
}

无论如何,我是否可以将天线IsEnabled 的值转换为 KeyValuePair 变量数据类型?

4

3 回答 3

2

我想你想提取selectReaderas 键和AntennaIsEnabled

string[] parts = strA.Split('.');
var list = new List<KeyValuePair<string, string>>();
list.Add(new KeyValuePair<string, string>(part[0], part[2]));

需要添加一些错误检查...

于 2012-11-30T12:28:39.103 回答
0

听起来您想要一个如下所示的结果:

Dictionary<int, bool> AntennaIsEnabled = new Dictionary<int, bool>();
foreach( KeyValuePair<int,SOME_CLASS> reader in _readerDict )
{
  var selectedReader = reader;

  //From your example
  AntennaIsEnabled.Add(1, selectedReader.Value.Antenna1IsEnabled);
  AntennaIsEnabled.Add(2, selectedReader.Value.Antenna2IsEnabled);
  AntennaIsEnabled.Add(3, selectedReader.Value.Antenna3IsEnabled);
  AntennaIsEnabled.Add(4, selectedReader.Value.Antenna4IsEnabled);
}

然后,您可以使用 AntennaIsEnabled[2] 之类的语法访问每个天线的状态。

于 2012-11-30T12:56:31.510 回答
0

尝试使用FastMember库以字符串形式访问您的属性。

于 2012-11-30T19:05:41.290 回答