Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could also augment the extension method approach to incorporate state, in a pattern not unlike WPF's attached properties. </p> <p>Here is an example with minimum boilerplate. Note that no modification are required on the target classes, including adding interfaces, unless you need to deal with the target class polymorphically - in which case you end up with something very close to actual Multiple Inheritance.</p> <pre><code>// Mixin class: mixin infrastructure and mixin component definitions public static class Mixin { // ===================================== // ComponentFoo: Sample mixin component // ===================================== // ComponentFooState: ComponentFoo contents class ComponentFooState { public ComponentFooState() { // initialize as you like this.Name = "default name"; } public string Name { get; set; } } // ComponentFoo methods // if you like, replace T with some interface // implemented by your target class(es) public static void SetName&lt;T&gt;(this T obj, string name) { var state = GetState(component_foo_states, obj); // do something with "obj" and "state" // for example: state.Name = name + " the " + obj.GetType(); } public static string GetName&lt;T&gt;(this T obj) { var state = GetState(component_foo_states, obj); return state.Name; } // ===================================== // boilerplate // ===================================== // instances of ComponentFoo's state container class, // indexed by target object static readonly Dictionary&lt;object, ComponentFooState&gt; component_foo_states = new Dictionary&lt;object, ComponentFooState&gt;(); // get a target class object's associated state // note lazy instantiation static TState GetState&lt;TState&gt;(Dictionary&lt;object, TState&gt; dict, object obj) where TState : new() { TState ret; if(!dict.TryGet(obj, out ret)) dict[obj] = ret = new TState(); return ret; } } </code></pre> <p>Usage:</p> <pre><code>var some_obj = new SomeClass(); some_obj.SetName("Johny"); Console.WriteLine(some_obj.GetName()); // "Johny the SomeClass" </code></pre> <p>Note that it also works with null instances, since extension methods naturally do.</p> <p>You might also consider using a WeakDictionary implementation to avoid memory leaks caused by the collection's holding on to target class references as keys.</p>
    singulars
    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. This table or related slice is empty.
    1. 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