Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm adding an expression based form as well. Do note the horrendous boxing/unboxing which needs to be done due to the fact that it is a struct. </p> <p>But as one can see the format is quite nice:</p> <pre><code>var p2 = p.With(t =&gt; t.X, 4); var p3 = p.With(t =&gt; t.Y, 7).With(t =&gt; t.X, 5); // Yeah, replace all the values :) </code></pre> <p>And the method is really applicable to all kinds of types.</p> <pre><code>public void Test() { var p = new Position(8, 3); var p2 = p.With(t =&gt; t.X, 4); var p3 = p.With(t =&gt; t.Y, 7).With(t =&gt; t.X, 5); Console.WriteLine(p); Console.WriteLine(p2); Console.WriteLine(p3); } public struct Position { public Position(int X, int Y) { this._X = X; this._Y = Y; } private int _X; private int _Y; public int X { get { return _X; } private set { _X = value; } } public int Y { get { return _Y; } private set { _Y = value; } } public Position With&lt;T, P&gt;(Expression&lt;Func&lt;Position, P&gt;&gt; propertyExpression, T value) { // Copy this var copy = (Position)this.MemberwiseClone(); // Get the expression, might be both MemberExpression and UnaryExpression var memExpr = propertyExpression.Body as MemberExpression ?? ((UnaryExpression)propertyExpression.Body).Operand as MemberExpression; if (memExpr == null) throw new Exception("Empty expression!"); // Get the propertyinfo, we need this one to set the value var propInfo = memExpr.Member as PropertyInfo; if (propInfo == null) throw new Exception("Not a valid expression!"); // Set the value via boxing and unboxing (mutable structs are evil :) ) object copyObj = copy; propInfo.SetValue(copyObj, value); // Since struct are passed by value we must box it copy = (Position)copyObj; // Return the copy return copy; } public override string ToString() { return string.Format("X:{0,4} Y:{1,4}", this.X, this.Y); } } </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