【问题标题】:How to use getValue from GetProperty returned from the result of Task<T>如何使用从 Task<T> 结果返回的 GetProperty 中的 getValue
【发布时间】:2017-02-17 23:17:47
【问题描述】:

所以我的问题是,我想重构这段代码。 我需要检索 3 个值,SpentTimeHoursRemainingTimeHoursInitialEffortEstimateHours,它们是可为空的双精度(双精度?), 此值必须来自缓存(字典),如果缓存不存在,则从 db(存储库模式)获取。

字典中的值工作正常,问题是从存储库中获取值并使用反射。 FindFirstOrDefaultAsync 返回对象USTaskTask&lt;T&gt;

public partial class USTask
{
    public long Aid { get; set; }
    public Nullable<double> RemainingTimeHours { get; set; }
    public Nullable<double> SpentTimeHours { get; set; }
    public Nullable<double> InitialEffortEstimateHours { get; set; }
}

KpiService 类

 public double GetEstimatedStoryPoint(
            List<CrossReference> result,
            string propertyName,
            string pattern,
            long Aid,
            bool isCache,
            bool isUserStory = false)
        {

            if (!isCache)
            {
                double? value = 0;
                //ERROR - 
                value = _userStoryTaskRepository
                    .FindFirstOrDefaultAsync(id => id.Aid == ids.Aid)
                    .Result
                    .GetType()
                    .GetProperty(propertyName)
                    .GetValue(value , null) as double?;
            }
            else
            {
                //Working fine
                value = GetValuesForEstimatedStoryPoint(GraphTable._USTask, propertyName, ids.Aid);
            }
......

在 KpiVieModel.cs 中

我在构造函数中注入了 kpiService,我这样调用, 我将 3 propertyName 作为参数传递 SpentTimeHours, RemainingTimeHours, InitialEffortEstimateHours

那我这样叫

// This Work
//In cache
sumSpentTime += _kpiService.GetEstimatedStoryPoint(crosReferenceInCache, "SpentTimeHours", pattern, Aid, true, isUserStory);
sumRemainigTime += _kpiService.GetEstimatedStoryPoint(crosReferenceInCache, "RemainingTimeHours", pattern, Aid, true, isUserStory);
sumEstimated += _kpiService.GetEstimatedStoryPoint(crosReferenceInCache, "InitialEffortEstimateHours", pattern, Aid, true, isUserStory);


This not..
//not in cache , db access 
sumSpentTime += _kpiService.GetEstimatedStoryPoint(crosReferenceNotInCache, "SpentTimeHours", pattern, Aid, false, isUserStory);
sumRemainigTime += _kpiService.GetEstimatedStoryPoint(crosReferenceNotInCache, "RemainingTimeHours", pattern, Aid, false, isUserStory);
sumEstimated += _kpiService.GetEstimatedStoryPoint(crosReferenceNotInCache, "InitialEffortEstimateHours", pattern, Aid, false, isUserStory);

问题是对象与目标源的类型不匹配。

我不想这样做 n 次。

SpentTimeHours = _userStoryTaskRepository.FindFirstOrDefaultAsync(id => id.Aid == ids.Aid).Result.SpentTimeHours;
RemainingTimeHours = _userStoryTaskRepository.FindFirstOrDefaultAsync(id => id.Aid == ids.Aid).Result.RemainingTimeHours;
InitialEffortEstimateHours = _userStoryTaskRepository.FindFirstOrDefaultAsync(id => id.Aid == ids.Aid).Result.InitialEffortEstimateHours;

【问题讨论】:

  • 什么是foundA?您不需要将变量分配给Result,然后将其传递给GetValue吗?
  • 您好 Quantic,抱歉复制和粘贴错误,FoundA,不存在,实际上我需要将结果值传递给变量值。
  • 我很确定它必须是 2 个步骤:var myObject = _userStoryTaskRepository.FindFirstOrDefaultAsync(id =&gt; id.Aid == ids.Aid).Result; value = myObject.GetType().GetProperty(propertyName).GetValue(myObject, null) as double?;
  • 五星级Quantic,正在工作,我觉得很愚蠢,很容易,谢谢。

标签: c# gettype getvalue getproperty


【解决方案1】:

如果我错了,请纠正我。在这种情况下,您想处理更好的null 结果吗?如果这是真的,我将使用null-coalescing operator 处理null,如下所示:

sumSpentTime += _kpiService.GetEstimatedStoryPoint(crosReferenceNotInCache, "SpentTimeHours", pattern, Aid, false, isUserStory) ?? 0;
sumRemainigTime += _kpiService.GetEstimatedStoryPoint(crosReferenceNotInCache, "RemainingTimeHours", pattern, Aid, false, isUserStory) ?? 0;
sumEstimated += _kpiService.GetEstimatedStoryPoint(crosReferenceNotInCache, "InitialEffortEstimateHours", pattern, Aid, false, isUserStory) ?? 0; 

【讨论】:

  • 您好 Kat1330 ,您的回答也正确,谢谢。
猜你喜欢
  • 2016-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-16
  • 1970-01-01
  • 2016-10-05
  • 1970-01-01
  • 2014-08-14
相关资源
最近更新 更多