Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use <a href="http://msdn.microsoft.com/en-US/library/system.delegate.createdelegate.aspx" rel="nofollow"><code>Delegate.CreateDelegate</code></a> and the <a href="http://msdn.microsoft.com/en-US/library/system.reflection.methodbase.getparameters.aspx" rel="nofollow"><code>GetParameters</code></a> method to do what you want.</p> <pre><code>public class RuleSet : IRuleSet { public IEnumerable&lt;Func&lt;SubjectA, Conclusion&gt;&gt; SubjectARules { get; set; } public IEnumerable&lt;Func&lt;SubjectB, Conclusion&gt;&gt; SubjectBRules { get; set; } } public static class RuleEngine { public static IEnumerable&lt;IRuleSet&gt; RuleSets() // removed contexts parameter for brevity { var result = from t in Assembly.GetExecutingAssembly().GetTypes() where t.GetCustomAttributes(typeof(RuleSetAttribute), true).Any() let m = t.GetMethods().Where(m =&gt; m.GetCustomAttributes(typeof(RuleAttribute)).Any()).ToArray() select new RuleSet { SubjectARules = CreateFuncs&lt;SubjectA&gt;(m).ToList(), SubjectBRules = CreateFuncs&lt;SubjectB&gt;(m).ToList() }; return result; } } // no error checking for brevity // TODO: use better variable names public static IEnumerable&lt;Func&lt;T, Conclusion&gt;&gt; CreateFuncs&lt;T&gt;(MethodInfo[] m) { return from x in m where x.GetParameters()[0].ParameterType == typeof(T) select (Func&lt;T, Conclusion&gt;)Delegate.CreateDelegate(typeof(Func&lt;T, Conclusion&gt;), null, x); } </code></pre> <p>Then you can use it like this:</p> <pre><code>var sa = new SubjectA(); foreach (var ruleset in RuleEngine.RuleSets()) { foreach (var rule in ruleset.SubjectARules) { var conclusion = rule(sa); // do something with conclusion } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload