-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflection - Dynamic filter and property read.linq
64 lines (56 loc) · 2.03 KB
/
Reflection - Dynamic filter and property read.linq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<Query Kind="Program" />
void Main()
{
List<MyClass> data = new List<MyClass>()
{
new MyClass() { ID = 1, Name = "Sue", CreatedDt = new DateTime(2016, 1, 1), Alpha = "blah", Beta = null, Gamma = 100.01M, Zeta = 2.123456 },
new MyClass() { ID = 2, Name = "Adam", CreatedDt = new DateTime(2016, 2, 1), Alpha = "blah", Beta = null, Gamma = 100.01M, Zeta = 2.123456 },
new MyClass() { ID = 3, Name = "Fred", CreatedDt = new DateTime(2016, 3, 1), Alpha = "blah", Beta = new DateTime(2015, 7, 9), Gamma = 200.01M, Zeta = 2.123456 },
new MyClass() { ID = 4, Name = "Anne", CreatedDt = new DateTime(2016, 4, 1), Alpha = "blah", Beta = null, Gamma = 100.01M, Zeta = 11.1 },
new MyClass() { ID = 5, Name = "Jen", CreatedDt = new DateTime(2016, 5, 1), Alpha = "blah", Beta = new DateTime(2015, 5, 3), Gamma = 101.01M, Zeta = 2.123456 },
new MyClass() { ID = 6, Name = "Angel", CreatedDt = new DateTime(2016, 6, 1), Alpha = "blah", Beta = null, Gamma = 100.01M, Zeta = 7.123456 },
new MyClass() { ID = 7, Name = "Gary", CreatedDt = new DateTime(2016, 7, 1), Alpha = "blah", Beta = null, Gamma = 100.01M, Zeta = 9.123456 }
};
foreach (var v in data)
{
Console.WriteLine(GetPropertyValue(v, "Zeta"));
}
foreach (var v in data)
{
if (SearchPropertyValues(v, "2.12"))
{
Console.WriteLine(v.ID);
}
}
}
class MyClass
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime CreatedDt { get; set; }
public string Alpha { get; set; }
public DateTime? Beta { get; set; }
public decimal Gamma { get; set; }
public double Zeta { get; set; }
}
object GetPropertyValue(object o, string propertyName)
{
return o.GetType().GetProperty(propertyName).GetValue(o);
}
bool SearchPropertyValues(object o, string searchText)
{
bool rc = false;
Type t = o.GetType();
foreach (var p in t.GetProperties())
{
//Console.WriteLine(p.Name);
object v = t.GetProperty(p.Name).GetValue(o);
//todo: Contains isn't case insensitive
if (v != null && v.ToString().Contains(searchText))
{
rc = true;
break;
}
}
return rc;
}