Is it possible to use the link return function in C # 7.0, to define a general function that can perform both comparison and field updating in two instances of an object? I imagine something like this:
void UpdateIfChanged<TClass, TField>(TClass c1, TClass c2, Func<TClass, TField> getter) { if (!getter(c1).Equals(getter(c2)) { getter(c1) = getter(c2); } }
An example of the intended use:
Thing thing1 = new Thing(field1: 0, field2: "foo"); Thing thing2 = new Thing(field1: -5, field2: "foo"); UpdateIfChanged(thing1, thing2, (Thing t) => ref t.field1); UpdateIfChanged(thing1, thing2, (Thing t) => ref t.field2);
Is it possible to specify a type of Func or any type restriction of a generic type that will make this valid by requiring getter to return the link? I tried Func<TClass, ref TField> , but it is not a valid syntax.
novog source share