I’m quite new at C# Generic Classes so I don’t know if we can update the generic property of a generic class dynamically?
Let say I have a class
A { long id; long count; }
Can we write an extension that update the given property by 1 so
A.AddOne(a => a.id); will change A {id=1, count=1} to A {id=2, count=1}
and
A.AddOne(a => a.count); will change A {id=1, count=1} to A {id=1, count=2}
Any helps would be appreciated!
If I understand your question correctly, you want to be able to declare which property to update by giving the
AddOnemethod a lambda expression. In this case, it’s possible — you can write an extension method that takes anExpression<T>, retrieves from this expression the property access expression, and uses Reflection to update that property on theAobject.The following is a very rough prototype of the above: