, mixin-like ( ):
public interface MPropertySettable { }
public static class PropertySettable {
public static void SetValue<T>(this MPropertySettable self, string name, T value) {
self.GetType().GetProperty(name).SetValue(self, value, null);
}
}
public class Foo : MPropertySettable {
public string Bar { get; set; }
public int Baz { get; set; }
}
class Program {
static void Main() {
var foo = new Foo();
foo.SetValue("Bar", "And the answer is");
foo.SetValue("Baz", 42);
Console.WriteLine("{0} {1}", foo.Bar, foo.Baz);
}
}
That way you can reuse this logic in many different classes without sacrificing your valuable base class.
In VB.NET:
Public Interface MPropertySettable
End Interface
Public Module PropertySettable
<Extension()> _
Public Sub SetValue(Of T)(ByVal self As MPropertySettable, ByVal name As String, ByVal value As T)
self.GetType().GetProperty(name).SetValue(self, value, Nothing)
End Sub
End Module
source
share