2010-07-15 65 views
0

我想在C#中創建一個鍵值對的集合,其中鍵是ASP.net控件的屬性(例如ID),值是該屬性的值。我想這樣做,以便稍後可以迭代集合並查看給定的控件是否在我的集合中具有屬性(並且控件中的屬性值與我的集合中定義的值相匹配)。任何建議最好的方式來做到這一點?謝謝你的幫助。收藏物業=>價值?

僞代碼示例:

Properties[] = new Properties[] {new Property(){name="ID",value="TestControl1"}, new Property(){name = "Text",value="Type text here"}} 

private bool controlContainsProperties(Control control){ 
    foreach(Property Property in Properties[]) 
    { 
     if((control does not contain property) || (control property value != Property.Value)) 
     return false; 
    } 
return true; 
} 

回答

0

沒有測試這一點,但這裏是我去:

public bool HasProperty(object target, IDictionary<string, object> values) 
    { 
     var targetType = target.GetType(); 
     return values.All(kvp => 
      { 
       var property = targetType.GetProperty(kvp.Key); 
       if (property != null) 
       { 
        var value = property.GetValue(target, null); 
        if (value != null) 
         return value.Equals(kvp.Value); 
       } 
       return false; 
      }); 
    } 
0

我的第一個想法就是使用「標籤」性質,但後來我意識到,有在APS.NET控制沒有標籤。但是,有關於標籤的回答question

在同一個線程中,有'屬性'屬性圖的解決方案 - 看起來很有前景。