【问题标题】:C# How do I extract the value of a property in a PropertyCollection?C#如何提取PropertyCollection中属性的值?
【发布时间】:2011-11-02 00:09:21
【问题描述】:

如何提取 PropertyCollection 中属性的值?

如果我在下面一行中的“属性”上深入了解是视觉工作室,我可以看到该值,但我如何阅读它?

foreach (string propertyName in result.Properties.PropertyNames)
{
  MessageBox.Show(ProperyNames[0].Value.ToString()); <--Wrong!
}

【问题讨论】:

  • “结果”的类型是什么?您想要属性中的哪个属性的值?

标签: c#


【解决方案1】:

使用上面的一些提示,我设法使用下面的代码得到了我需要的东西:

   ResultPropertyValueCollection values = result.Properties[propertyName];
    if (propertyName == "abctest")
    { 
      MessageBox.Show(values[0].ToString());
    }

谢谢大家。

【讨论】:

    【解决方案2】:

    试试这个:

    foreach (string propertyName in result.Properties.PropertyNames)
    {
        MessageBox.Show(result.Properties[propertyName].ToString());
    }
    

    或者这个:

    foreach (object prop in result.Properties)
    {
         MessageBox.Show(prop.ToString());
    }
    

    另外:框架中有几个不同的 PropertyCollections 类。这些示例基于 System.Data 类,但您也可能使用 System.DirectoryServices 类。但是,这些类都不是真正的“反射”。反射指的是不同的东西——即 System.Reflection 命名空间加上几个特殊的运算符。

    【讨论】:

    • 我需要proeprtyname后面的索引,即result.Properties[propertyName][0].ToString()
    【解决方案3】:

    函数中的属性名是大写的吗?

    再次阅读,我不得不承认对于所有这些属性你所追求的东西有点困惑。这是您所追求的类属性值还是实例?

    【讨论】:

      【解决方案4】:

      Vb.NET

      For Each prop As String In result.Properties.PropertyNames
                      MessageBox.Show(result.Properties(prop).Item(0), result.Item(i).Properties(prt).Item(0))
      Next
      

      我认为 C# 看起来像这样......

      foreach (string property in result.Properties.PropertyNames)
      {
        MessageBox.Show(result.Properties[property].Item[0]);
      }
      

      如上所述,框架中有几个不同的属性集合。

      【讨论】:

        【解决方案5】:

        我不确定您要的是什么,但我认为问题在于您看到的是属性名称而不是它们的值?

        如果是这样,原因是您正在枚举 PropertyCollection.PropertyNames 集合,而不是 PropertyCollection.Values 集合。试试这样的:

        foreach (object value in result.Properties.Values)
        {  
            MessageBox.Show(property.ToString());
        }
        

        我假设这个问题指的是 System.DirectoryServices.PropertyCollection 类,而不是 System.Data.PropertyCollection,因为引用了 PropertyNames,但现在我不太确定。如果问题与 System.Data 版本有关,请忽略此答案。

        【讨论】:

          【解决方案6】:

          如果你将值集合放在你的“if”中,你只会在你真正需要它时才检索它,而不是每次通过循环。只是一个建议... :)

          【讨论】:

            【解决方案7】:

            PropertyNames 在其他地方不是大写的,下面的代码可以工作并且会显示属性的名称,但我想读取该值。 'PropertyName' 只是一个字符串。

            foreach (string propertyName in result.Properties.PropertyNames)
            {
              MessageBox.Show(PropertyName.ToString());
            }
            

            【讨论】:

              【解决方案8】:

              试试:

              foreach (string propertyName in result.Properties.PropertyNames)
              {  MessageBox.Show(properyName.ToString()); <--Wrong!
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多