You have a list full of custom class items and want to find according to one property.Ok the best way is lambda functions(Or Not?).Other than lambda functions you can use reflection and use some code like:
public NeuronList FindByProperty(string PropertyName, object Value)
{
try
{
NeuronList foundItems = new NeuronList();
foreach (NeuronBase item in List)
{
System.Type itemType = item.GetType();
PropertyInfo[] itemProperties = itemType.GetProperties();
foreach (PropertyInfo info in itemProperties)
{
if (info.Name == PropertyName)
{
object itemValue = info.GetValue(item, null);
if (Convert.ChangeType(itemValue, info.PropertyType).Equals(Value))
{
foundItems.Add(item);
}
}
}
}
return foundItems;
}
catch (Exception ex)
{
throw new Exception("Can not find property", ex);
}
}
No comments:
Post a Comment