Created
October 16, 2012 19:12
-
-
Save ashga/3901324 to your computer and use it in GitHub Desktop.
Question 3 Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
public delegate void FoundString(string found); | |
public class WhatDoIDo | |
{ | |
private ILogger _logger = new Logger(); | |
private readonly List<object> _objects = new List<object>(); | |
public event FoundString Found; | |
public List<object> Do(List<object> source, string name) | |
{ | |
foreach (var obj in source) | |
{ | |
var type = obj.GetType(); | |
var sourceProperties = type.GetProperties(); | |
var found = false; | |
foreach (var sourceProperty in sourceProperties) | |
{ | |
if (sourceProperty.Name == name) | |
{ | |
found = true; | |
Found(name); | |
} | |
} | |
if (found) | |
{ | |
if (!Has(obj, name)) | |
{ | |
_objects.Add(obj); | |
} | |
} | |
} | |
return _objects; | |
} | |
private bool Has(object sourceItem, string propertyName) | |
{ | |
if (sourceItem == null) | |
{ | |
throw new Exception(); | |
_logger.Log("source is null"); | |
} | |
Boolean rv = false; | |
foreach (var obj in _objects) | |
{ | |
var objType = obj.GetType(); | |
var objPs = objType.GetProperties(); | |
foreach (var pi in objPs) | |
{ | |
if (pi.Name == propertyName) | |
{ | |
var sourceItemType = sourceItem.GetType(); | |
var sourceObjPs = sourceItemType.GetProperties(); | |
foreach (var itemPs in sourceObjPs) | |
{ | |
if (itemPs.Name == propertyName) | |
{ | |
if (itemPs.GetValue(sourceItem, null).ToString() == pi.GetValue(obj, | |
null).ToString()) | |
{ | |
rv = true; | |
} | |
} | |
} | |
} | |
} | |
} | |
return rv; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment