考虑这样的接口:
new Provider().For(myClass).ExcludeProperties("Height", "Width");
public IEditableStateProvider For(object target) {...}
public IEditableStateProvider ExcludePropertyNames(params string[] propertyNames) {...}
我想将params string[] propertyNames
arg替换为,params Expression<Func<object>>[] propertyNames
以便获得以下内容。
new Provider().For(myClass).ExcludeProperties(()=>Height, ()=>Width);
我见过类似的代码,所以我认为它应该可以工作,但我还没有得到它。我怎样才能让它工作?
编辑 - 这样做没有泛型
这是一个开源项目的一些代码,我正在研究类型推断在没有任何泛型的情况下工作的地方。我也想做同样的事情,但我看不到类型推断的来源(我确实看到它有效!)
// USAGE (here this is being called from code-behind of a 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) {
...
return config;
}
}
public class TrackingConfiguration
{
public TrackingConfiguration AddProperties(params Expression<Func<object>>[] properties) {
...
return this;
}
}
static class Services
{
public static readonly SettingsTracker Tracker = new SettingsTracker(ObjectStore);
}