Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Setting aside the question of how to dynamically attach a property or interface, it sounds like what you're attempting to do is augment existing classes with additional data. A very typical solution to this problem is to use a <code>Dictionary&lt;Something, SomethingExtra&gt;</code> and store it in some service class that maintains the mapping. Now, when you need access to SomethingExtra, you just ask the service class for the associated information.</p> <p>Advantages:</p> <ol> <li><p>The implementation is easier to understand and maintain than a solution using reflection and dynamic proxy generation.</p></li> <li><p>If you need to derive from the type being extended, the class can't be sealed. Associating information externally works fine with sealed classes.</p></li> <li><p>You can associate information without having to be responsible for the construction of the augmented object. You can take instances created anywhere and associate the new information.</p></li> </ol> <p>Disadvantages:</p> <ol> <li><p>You need to inject the service instance that maintains the mapping. You can do this via an IoC framework if you're using one, manual injection (usually passed through a constructor) or, if there's no other alternative, via a Singleton static accessor.</p></li> <li><p>If you have a very large number of instances and they are being rapidly created/removed, the Dictionary overhead could become noticeable. I suspect you would need some very heavy load before the overhead becomes noticeable compared to a proxying implementation.</p></li> </ol>
 

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