Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a long term solution to an ever expanding list of critera without having to add more if/then/else.</p> <p>While this code is complex to someone not used to designing in this manner, it allows you to keep your method dealing with criteria the same, and just register new delegates to handle additional criteria.</p> <p>The idea is to create a map of <code>Type</code> objects that hold delegates in which to execute. You can then register new delegates to execute based on new <code>Type</code>s as you generate them.</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Stackoverflow_4527626 { delegate void CriteraDelegate(params object[] args); class CriteraManager { private Dictionary&lt;Type, CriteraDelegate&gt; criterian = new Dictionary&lt;Type, CriteraDelegate&gt;(); public void RegisterCritera(Type type, CriteraDelegate del) { criterian[type] = del; } public void Execute(Object criteria, params object[] args) { Type type = criteria.GetType(); /// Check to see if the specific type /// is in the list. if (criterian.ContainsKey(type)) { criterian[type](args); } /// If it isn't perform a more exhaustive search for /// any sub types. else { foreach (Type keyType in criterian.Keys) { if (keyType.IsAssignableFrom(type)) { criterian[keyType](args); return; } } throw new ArgumentException("A delegate for Type " + type + " does not exist."); } } } interface InterfaceA { } interface InterfaceB1 : InterfaceA { } interface InterfaceB2 : InterfaceA { } interface InterfaceC { } class ClassB1 : InterfaceB1 { } class ClassB2 : InterfaceB2 { } class ClassC : InterfaceC { } class Program { static void ExecuteCritera1(params object[] args) { Console.WriteLine("ExecuteCritera1:"); foreach (object arg in args) Console.WriteLine(arg); } static void ExecuteCritera2(params object[] args) { Console.WriteLine("ExecuteCritera2:"); foreach (object arg in args) Console.WriteLine(arg); } static void Main(string[] args) { CriteraDelegate exampleDelegate1 = new CriteraDelegate(ExecuteCritera1); CriteraDelegate exampleDelegate2 = new CriteraDelegate(ExecuteCritera2); CriteraManager manager = new CriteraManager(); manager.RegisterCritera(typeof(InterfaceB1), exampleDelegate2); manager.RegisterCritera(typeof(InterfaceB2), exampleDelegate2); manager.RegisterCritera(typeof(InterfaceC), exampleDelegate1); ClassB1 b1 = new ClassB1(); ClassB2 b2 = new ClassB2(); ClassC c = new ClassC(); manager.Execute(b1, "Should execute delegate 2"); manager.Execute(b2, "Should execute delegate 2"); manager.Execute(c, "Should execute delegate 1"); } } } </code></pre>
 

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