
Untitled
By: a guest on
Apr 29th, 2012 | syntax:
None | size: 1.09 KB | hits: 13 | expires: Never
How to access a custom collection property by its property name
public class Property : IComparable<Property>
{
public string Name;
public string Value;
public string Group;
public string Id;
...
...
public int CompareTo(Property other)
{
return Name.CompareTo(other.Name);
}
}
Public List<Property> properties;
var myColor = properties["Color"].Value;
var props = properties.ToDictionary( x => x.Name );
Property prop = props["some name"];
public class PropertyCollection : List<Property>
{
public Property this[string name]
{
get
{
foreach (Property prop in this)
{
if (prop.Name == name)
return prop;
}
return null;
}
}
}
PropertyCollection col = new PropertyCollection();
col.Add(new Property(...));
Property prop = col["some name"];
Dictionary<string, Property> properties = new Dictionary<string, Property>();
//you add it like that:
properties[prop.Name] = prop;
//then access it like that:
var myColor = properties["Color"];