c# - Fluent WPF API for inherited properties -
c# - Fluent WPF API for inherited properties -
occasionally i'll have wpf c# code i'd write in "fluent" manner.
for example, might want setup window
containing scrollviewer
:
new window() .setcontent( new scrollviewer() .setcontent( ...))
to accomplish sort of api, i've been using extension methods such these:
static class fluentscrollviewer { public static scrollviewer setcontent(this scrollviewer obj, object val) { obj.content = val; homecoming obj; } } static class fluentwindow { public static window setcontent(this window obj, object val) { obj.content = val; homecoming obj; } }
now, window
, scrollviewer
both inherit content
property contentcontrol
. yet, i've had define setcontent
extension methods each class separately.
if seek instead:
static class fluentcontentcontrol { public static contentcontrol setcontent(this contentcontrol obj, object val) { obj.content = val; homecoming obj; } }
and utilize so:
new window().setcontent(...)
the setcontent
method doesn't homecoming window
of course.
is there way define setcontent
on contentcontrol
, have "right thing" avoid defining lots of individually speciallized methods similar except type?
you can utilize generics:
public static tcontrol setcontent<tcontrol>(this tcontrol obj, object val) tcontrol : contentcontrol { obj.content = val; homecoming obj; }
c# wpf fluent fluent-interface
Comments
Post a Comment