I have a complex class whose fields can be lists dictionaries and any other classes. Some basic data types are int , bool, string or some enums. I need to created a functions that takes an instance of that class and a string and return true or false depending if there is a mentioning of the string.
public class Record
{
public Dictionary<string,int> Counts { get; set; }
public Dictionary<string,list<string>> Syns {get; set;}
public string Name { get; set; }
public KeyValuePair<string,KeyValuePair<string,bool>> Stuff { get; set; }
//etc
}
Here is what the method should do
public static bool IsValuePresent(Object o, string keyword)
{
//cycle through all possible string values of o and check if keyword is present.
//return true if so, otherwise false
}
possible call:
Record record = dbAccess.GetCurrent();
bool flag = IsValuePresent(record, "Name");
Remark: can use ToJSON but this will verify if "name" is present among names of of object properties, but it should only verify values.
You should be able to use recursion and use System.Reflection. Here is a good post:
Reflection – Iterate object's properties recursively within my own assemblies (Vb.Net/3.5)
Look at: