C# - 存储然后更新属性
晚上,
我编写了一个传输应用程序,需要执行更新,然后更新与该更新相对应的日期。 即
string content = string.Empty;
IIvdManager manager;
DateTime configDate;
if (table == Tables.Asset)
{
content = WebService.GetTblAsset(companyId);
manager = ivdData.Instance.Asset;
configDate = ivdConfig.LAST_UPDATE_ASSET;
}
else if (table == Tables.Site)
{
content = WebService.GetTblSite(companyId);
manager = ivdData.Instance.Site;
configDate = ivdConfig.LAST_UPDATE_SITE;
}
else if (table...)
{
...
}
if (content.Length > 0)
{
if (UpdateManager(manager, content))
{
configDate = DateTime.Now;
}
}
我想要的是 configDate 属性更新我的 ivdConfig 静态类中相应的日期获取/设置。
我怎样才能做到这一点?
Evening all,
I've written a transfer application that needs to perform an update, then update a date corresponding to that update. I.e.
string content = string.Empty;
IIvdManager manager;
DateTime configDate;
if (table == Tables.Asset)
{
content = WebService.GetTblAsset(companyId);
manager = ivdData.Instance.Asset;
configDate = ivdConfig.LAST_UPDATE_ASSET;
}
else if (table == Tables.Site)
{
content = WebService.GetTblSite(companyId);
manager = ivdData.Instance.Site;
configDate = ivdConfig.LAST_UPDATE_SITE;
}
else if (table...)
{
...
}
if (content.Length > 0)
{
if (UpdateManager(manager, content))
{
configDate = DateTime.Now;
}
}
What I want is for the configDate property to update the corresponding Date Get/Set in my ivdConfig Static Class.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您考虑过状态模式吗?
对表(表变量)进行子类化并添加一个虚拟方法(也许是 Update()?),然后在每个特定的表类型中重写该方法。 这将完全删除 else if,因为它将变成:
将您需要的任何对象传递给此调用,然后从表中获取值(因为它可以在 Update() 的实现中更新其自己的特定日期属性)。
如果我的说法有误,我深表歉意,但说实话,我并不能 100% 确定您的要求。
Have you considered the state pattern?
Subclass your table (the table variable) and add a virtual method (Update() perhaps?) that you then override in each specific table type. This will completely remove the else ifs as it will just become:
Pass down any objects you need to this call and then get the value back from the table (as it can update its own specific date property within its implementation of Update()).
I apologise if I have the wrong end of the stick but i'm not 100% sure what you are asking to be honest.
并不是说我认为这是好的代码,但这就是您正在寻找的吗?
Not that I think this is good code, but is this what you are looking for?
这听起来就像你需要这样做一样简单:
这意味着我几乎肯定是错的......
It sounds as simple as you need to do this:
Which means I'm almost certainly wrong....
DateTime 类型是值类型,因此上面的代码只是将 ivdConfig.LAST_UPDATE_SITE 的值复制到 dateToUpdate,而不是引用。
要解决您的问题,您可以执行以下操作:
DateTime type is a value type, so the code above will just copy the value of ivdConfig.LAST_UPDATE_SITE to dateToUpdate and not the reference to.
To solve you problem you can do the following:
确实有 PropertyInfo.SetValue 方法。
Well there does happen to be the PropertyInfo.SetValue method.