具有多个参数的通用方法

时间:2016-09-07 20:22:20

标签: c# .net

我正在尝试为方法CheckResult编写一个Generic方法,以便它可以在不同的类中使用。例如,如果我有一个泛型方法,那么唯一要改变的是classname。这是ClassA,另一种方法可以传递classB。

public bool CheckResult(Guid Id, List<ClassA> model,List<ClassA> existingEntities)
{
    var ids = existingEntities?.Select(x => x.Id).Except(model.Select(x => x.Id)).ToList();
    var check = existingEntities?.Where(o => ids.Any(c => c == o.Id && o.EffectiveTo >= DateTime.Today)).ToList();
    check?.AddRange(model);
    var dateModel = check.Select(x => new TimeInterval(x.EffectiveFrom, x.EffectiveTo)).ToList();
    return true;
}

--------------这就是我试图做的事情--------------------- < / strong>

public static bool OpTest<T>(T model, T existingEntities, Guid t) where T : class
{
    //  var existingEntities = smRepository.GetStationMapping(t, StatusEnum.ALL); //smRepository.GetStationMapping(t, StatusEnum.ALL);
    var ids = existingEntities?.Select(x => x.Id).Except(model.Select(x => x.Id)).ToList();
    var check = existingEntities?.Where(o => ids.Any(c => c == o.Id && o.EffectiveTo >= DateTime.Today)).ToList();
    check?.AddRange(model);
    var dateModel = check.Select(x => new TimeInterval(x.EffectiveFrom, x.EffectiveTo)).ToList();
    return true;
}

这是我第一次写通用任何帮助表示赞赏。代码错了吗?

2 个答案:

答案 0 :(得分:2)

我在这里看到一个问题。您尝试访问T类型对象的成员,但T是未知类型;我们无法保证会让您尝试访问该会员。 C#并不喜欢这样。

我在这里看到两个选项:

强大的选项

使用适当的成员创建一个界面IMyInterface。然后写下:

public static bool OpTest(IEnumerable<IMyInterface> model, IEnumerable<IMyInterface> existingEntities, Guid t)
{
    List<SomeType> ids = existingEntities?.Select(x => x.Id).Except(model.Select(x => x.Id)).ToList();
    List<IMyInterface> check = existingEntities?.Where(o => ids.Any(c => c == o.Id && o.EffectiveTo >= DateTime.Today)).ToList();

    check?.AddRange(model);

    List<TimeInterval> dateModel = check.Select(x => new TimeInterval(x.EffectiveFrom, x.EffectiveTo)).ToList();

    return true;
}

快速和脏选项

请改用dynamic。请注意,如果您使用此代码,如果您犯了任何错误,您的代码将在运行时中断。如果你不希望它像那样打破,那么就不要使用这个选项。

public static bool OpTest(IEnumerable<dynamic> model, IEnumerable<dynamic> existingEntities, Guid t)
{
    List<dynamic> ids = existingEntities?.Select(x => x.Id).Except(model.Select(x => x.Id)).ToList();
    List<dynamic> check = existingEntities?.Where(o => ids.Any(c => c == o.Id && o.EffectiveTo >= DateTime.Today)).ToList();

    check?.AddRange(model);

    List<dynamic> dateModel = check.Select(x => new TimeInterval(x.EffectiveFrom, x.EffectiveTo)).ToList();

    return true;
}

答案 1 :(得分:0)

public static bool OpTest<T>(List<T> model, List<T> existingEntities, Guid t) where T : class
    {

        //  var existingEntities = smRepository.GetStationMapping(t, StatusEnum.ALL); //smRepository.GetStationMapping(t, StatusEnum.ALL);


        var ids = existingEntities?.Select(x => x.Id).Except(model.Select(x => x.Id)).ToList();

        var check = existingEntities?.Where(o => ids.Any(c => c == o.Id && o.EffectiveTo >= DateTime.Today)).ToList();


        check?.AddRange(model);


        var dateModel = check.Select(x => new TimeInterval(x.EffectiveFrom, x.EffectiveTo)).ToList();


        return true;
    }

将您的商品保留为列表,或者您无法使用linq。