使用表达式参数化属性名称
我有一段代码,其中如果该类存在,我将一个类的某些属性委托给另一个类。下面您可以看到 TimeProvider 上定义的开始时间(如果存在)。
public override DateTime StartTime
{
get
{
return (TimeProvider != null) ? TimeProvider.StartTime : base.StartTime;
}
set
{
if (TimeProvider != null)
{
TimeProvider.StartTime = value;
}
else
{
base.StartTime = value;
}
}
}
现在我的“问题”是我有更多的属性(如结束时间,当前时间),如果它不为空,我将委托给 TimeProvider 。所以我想要这样的代码:
public override DateTime StartTime
{
get
{
return GetTime(()=>StartTime);
}
set
{
SetTime(()=>StartTime)
}
}
其中 ()=>StartTime 表达式有时在 TimeProvider 上“评估”,有时在类本身上“评估”(它们共享一个基类)。这样的事情可以做吗?最好不使用反射。
I am having a piece of code in which i delegate certain properties of a class to another class if the class exists. Below you see the start time being defined on the TimeProvider if it exists.
public override DateTime StartTime
{
get
{
return (TimeProvider != null) ? TimeProvider.StartTime : base.StartTime;
}
set
{
if (TimeProvider != null)
{
TimeProvider.StartTime = value;
}
else
{
base.StartTime = value;
}
}
}
Now my 'problem' is that i have a lot more properties (like endtime,currentime) i delegate to the TimeProvider if it is not null. So i would like to have code like:
public override DateTime StartTime
{
get
{
return GetTime(()=>StartTime);
}
set
{
SetTime(()=>StartTime)
}
}
Where the ()=>StartTime expression is sometimes 'evaluated' on the TimeProvider and sometimes on the class itself (they share a baseclass). Can something like this be done? Preferably without using reflection.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
蛋糕
Cake