111,089
社区成员




public class Tree
{
//节点文本
public string Text { get; set; }
//子节点列表
public List<BaseData> TreeNodes { get; set; }
//当前节点对应的数据列表,每个节点对应的数据类型可能不同,有可能是A,有可能是B
public List<object> Items { get; set; }
}
public class BaseData
{
public bool IsMatch(string value)
{
var ps = this.GetType().GetProperties();
foreach (var p in ps)
{
System.Reflection.PropertyInfo pi = this.GetType().GetProperty(p.Name);
string _value = pi.GetValue(this, null).ToString();
if (_value.Contains(value))//----------------条件
return true;
}
return false;
}
}
public class Data : BaseData
{
public string ID { get; set; }
public string Name { get; set; }
}
private void button1_Click(object sender, EventArgs e)
{
Tree t = new Tree();
t.TreeNodes = new List<BaseData>();
for(int i =0; i < 100000; i ++)
{
Data d = new Data();
d.ID = i.ToString();
d.Name = "Name" + i.ToString();
t.TreeNodes.Add(d);
}
var l = t.TreeNodes.FindAll(p => p.IsMatch("5") == true);
var l2 = t.TreeNodes.FindAll(p => p.IsMatch("Name") == true);
}
不知道这样行不行,还是用了反射。
Type t = tc.GetType();//获得该类的Type
//再用Type.GetProperties获得PropertyInfo[],然后就可以用foreach 遍历了
foreach (PropertyInfo pi in t.GetProperties())
{
object value1 = pi.GetValue(tc, null));//用pi.GetValue获得值
string name = pi.Name;//获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作
//进行你想要的操作
}