我有一个复杂的类,其字段可以是列表字典和任何其他类。一些基本的数据类型是int
,bool
,string
或某些枚举。我需要创建一个函数,该函数接受该类的一个实例和一个字符串,并根据是否提及字符串返回true或false。多维对象内的深度搜索
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
}
这里是什么方法应该做
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
}
可能的呼叫:
Record record = dbAccess.GetCurrent();
bool flag = IsValuePresent(record, "Name");
备注:可以使用ToJSON
但是这将验证是否"name"
存在的对象属性的名称中,但它应该只验证值。
您的代码不会像编写的那样编译 - 请发布“Record”的真实代码,或至少一个工作示例... –