Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I found this old question while researching my possible options in the same situation. I figured I should update it in case anyone else stumbles on to it:</p> <p>Another option (not available when Paul offered his solution since .Net 4 wasn't out yet) is to use the same strategy, but instead of implementing it using CustomTypeDescriptors, use a combination of generics, dynamic objects and reflection to achieve the same effect.</p> <p>In this case, you define a class</p> <pre><code>class Mutable&lt;ImmutableType&gt; : DynamicObject { //... } </code></pre> <p>It's constructor takes an instance of the immutable type and a delegate that constructs a new instance of it out of a dictionary, just like in Paul's answer. The difference here, however, is that you override the TryGetMember and TrySetMember to populate an internal dictionary that you're eventually going to use as the argument for the constructor-delegate. You use reflection in order to verify that the only properties that you're accepting are those that are actually implemented in ImmutableType.</p> <p>Performance wise, I wager that Paul's answer is faster, and doesn't involve dynamic objects, which are known to put C# developers into fits. But the implementation for this solution is also a little simpler, because Type Descriptors are a bit arcane.</p> <hr> <p>Here's the requested proof-of-concept / example implementation:</p> <p><a href="https://bitbucket.org/jwrush/mutable-generic-example" rel="nofollow noreferrer">https://bitbucket.org/jwrush/mutable-generic-example</a></p>
 

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