Thursday, 15 January 2015

c# - arg array of Expression<Func> as part of a fluent interface -



c# - arg array of Expression<Func<object>> as part of a fluent interface -

consider interface this:

new provider().for(myclass).excludeproperties("height", "width"); public ieditablestateprovider for(object target) {...} public ieditablestateprovider excludepropertynames(params string[] propertynames) {...}

i want replace params string[] propertynames arg params expression<func<object>>[] propertynames instead have following.

new provider().for(myclass).excludeproperties(()=>height, ()=>width);

i have seen code similar think should work, not getting yet. how can work?

edit - doing without generics

here code open source project looking @ type inference working without generics. looking same don't see type inference coming (i do see working though!)

// usage (here beingness called code-behind of wpf window private void trackselectedtab() { services.tracker.configure(tabcontrol) .addproperties(() => tabcontrol.selectedindex); services.tracker.applystate(tabcontrol); } private void trackmainwindow() { services.tracker.configure(this) .addproperties( () => height, () => width, () => left, () => top, () => windowstate) .setkey("mainwindow") .setmode(persistmodes.automatic); services.tracker.applystate(this); } // collab classes public class settingstracker { public trackingconfiguration configure(object target) { ... homecoming config; } } public class trackingconfiguration { public trackingconfiguration addproperties(params expression<func<object>>[] properties) { ... homecoming this; } } static class services { public static readonly settingstracker tracker = new settingstracker(objectstore); }

you should create generic provider class in addittion non-generic one, can take advantage of type inference , type safety:

interface:

interface ieditablestateprovider<t> { ieditablestateprovider<t> for(t target); ieditablestateprovider<t> excludepropertynames(params expression<func<t, object>>[] excludedproperties); }

dummy implementation:

class provider<t> : ieditablestateprovider<t> { public ieditablestateprovider<t> for(t target) { // dummy homecoming this; } public ieditablestateprovider<t> excludepropertynames(params expression<func<t, object>>[] excludedproperties) { // dummy homecoming this; } } class provider { // generic mill method create utilize of type inference public static ieditablestateprovider<t> for<t>(t obj) { homecoming new provider<t>().for(obj); } }

usage:

var myclass = new list<object>(); // or whatever provider.for(myclass).excludepropertynames(x => x.count);

the type t infered when phone call .for(myclass), , can access properties of type t in type safe manner via lambda when calling excludepropertynames.

if want non-generic version:

interface ieditablestateprovider { ieditablestateprovider for(object target); ieditablestateprovider excludepropertynames(params expression<func<object>>[] excludedproperties); } class provider : ieditablestateprovider { public ieditablestateprovider for(object target) { // dummy homecoming this; } public ieditablestateprovider excludepropertynames(params expression<func<object>>[] excludedproperties) { // dummy homecoming this; } } var myclass = new list<object>(); new provider().for(myclass).excludepropertynames(() => myclass.count);

but please note comment below.

c# lambda expression-trees fluent-interface

No comments:

Post a Comment