// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.CodeDom.Compiler; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using Microsoft.CSharp; namespace E { public class Data { public string V1 { get; set; } public string V2 { get; set; } public override string ToString() { return string.Format("{{V1={0},V2={1}}}", V1, V2); } } public class Data1 { public Data A { get; set; } public string B { get; set; } public string C { get; set; } public override string ToString() { return string.Format("{{A={0},B={1},C={2}}}", A, B, C); } } public class Data2 { public Data D { get; set; } public string E { get; set; } public override string ToString() { return string.Format("{{D={0},E={1}}}", D, E); } } public interface IFilter { bool Accept(Dictionary filter, T o); } public static class Util { public static TV Get(this Dictionary d, TK key) where TV : class { if(d.ContainsKey(key)) { return d[key]; } else { return null; } } private static Dictionary cache = new Dictionary(); private static void GetProperties(Type typ, string prefix, List result) { foreach(PropertyInfo pi in typ.GetProperties()) { string propnam = prefix + (prefix.Length > 0 ? "." : "") + pi.Name; if(pi.PropertyType.FullName.StartsWith("System.")) // <---- consider a better test for whether to recurse { result.Add(propnam); } else { GetProperties(pi.PropertyType, propnam, result); } } } private const string SOURCE_TEMPLATE = @"using System; using System.Collections.Generic; using {2}; namespace Gen {{ public class {0}Filter : IFilter<{0}> {{ public bool Accept(Dictionary filter, {0} o) {{ return {1}; }} }} }}"; private const string TEST_TEMPLATE = @"(o.{0} == (filter.Get(""{0}"") ?? o.{0}))"; private static object GenerateFilter(Type typ) { List proplist = new List(); GetProperties(typ, "", proplist); StringBuilder sb = new StringBuilder(); foreach(string s in proplist) { if(sb.Length > 0) { sb.Append(" && "); } sb.Append(string.Format(TEST_TEMPLATE, s)); } string src = string.Format(SOURCE_TEMPLATE, typ.Name, sb.ToString(), typ.Namespace); //Console.WriteLine(src); CodeDomProvider comp = new CSharpCodeProvider(); CompilerParameters param = new CompilerParameters(); param.GenerateInMemory = true; param.ReferencedAssemblies.Add("System.Reflection.dll"); param.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location); CompilerResults res = comp.CompileAssemblyFromSource(param, src); /* foreach(CompilerError line in res.Errors) { Console.WriteLine(line); } */ Assembly asm = res.CompiledAssembly; return asm.CreateInstance("Gen." + typ.Name + "Filter"); } public static List Filter(this List lst, Dictionary filtervals) { Type typ = typeof(T); IFilter filter; if(cache.ContainsKey(typ.FullName)) { filter = (IFilter)cache[typ.FullName]; } else { filter = (IFilter)GenerateFilter(typ); cache.Add(typ.FullName, filter); } return lst.Where(o => filter.Accept(filtervals, o)).ToList(); } } public class Program { public static void Main(string[] args) { List lst1 = new List { new Data1 { A = new Data { V1 = "1AV1", V2 = "1AV2" }, B = "1B", C = "1C" }, new Data1 { A = new Data { V1 = "2AV1", V2 = "2AV2" }, B = "2B", C = "2C" }, new Data1 { A = new Data { V1 = "3AV1", V2 = "3AV2" }, B = "3B", C = "3C" } }; List lst2 = new List { new Data2 { D = new Data { V1 = "4DV1", V2 = "4DV2" }, E = "4E" }, new Data2 { D = new Data { V1 = "5DV1", V2 = "5DV2" }, E = "5E" }, new Data2 { D = new Data { V1 = "6DV1", V2 = "6DV2" }, E = "6E" } }; foreach(Data1 o in lst1) { Console.WriteLine(o); } foreach(Data2 o in lst2) { Console.WriteLine(o); } Dictionary flt1 = new Dictionary(); flt1.Add("A.V2", "2AV2"); foreach(Data1 o in lst1.Filter(flt1)) { Console.WriteLine(o); } Dictionary flt2 = new Dictionary(); flt2.Add("A.V1", "6DV1"); flt2.Add("E", "6E"); foreach(Data2 o in lst2.Filter(flt2)) { Console.WriteLine(o); } } } }