【发布时间】:2013-01-08 20:19:53
【问题描述】:
我有一个复杂的类,它的字段可以是列表字典和任何其他类。一些基本数据类型是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"是否存在于对象属性的名称中,但它应该只验证值。
【问题讨论】:
-
您的代码不会按照所写的那样编译 - 请发布“记录”的真实代码,或者至少是一个工作示例...
标签: c#