FUNC<>使用Interface发送不同类型的Func<>

时间:2017-03-21 04:50:42

标签: c#

以下是基本示例的伪代码:

// Cannot touch
public class QAS
{
    public IEnumerable<T> Execute<T>(IEnumerable<Point> i) where T : class, IPointResults, new()
    {
        //returns object using IPointResults
    }
    public IEnumerable<T> Execute<T>(IEnumerable<Seg> i) where T : class, ISegResults, new()
    {
        //returns object using ISegResults
    }
}


public interface IPointResults
{
    int turtle;
}

public interface ISegResult
{
    double bird;
}

public interface PointSeg
{
    string id;
}

public class Point : PointSeg
{
    string id;
    int x;
    int y;
}

public class Seg : PointSeg
{
    string id;
    string q;
}

我有两个非常相似的方法,我想利用那里的通用where子句,采用相似的代码并将其合并为一个:

public IEnumerable<T> GetAttributes<T>(IEnumerable<Point> pts) where T : class, IPointResults, new()
{
    var queryAttributeSet = new QAS();

    var rv = new List<T>();
    var batches = pts.Batch(25);

    Log.Debug($"Attempting to get {total} records using {batches.Count()} requests");

    foreach (var batch in batches)
    {
        var results = queryAttributeSet.Execute<T>(batch);
        rv.AddRange(results);
    }

    return rv;
}

public IEnumerable<T> GetAttributes<T>(IEnumerable<Seg> segments) where T : class, ISegResults, new()
{
    var queryAttributeSet = new QAS();

    var rv = new List<T>();
    var batches = segments.Batch(25);

    Log.Debug($"Attempting to get {total} records using {batches.Count()} requests");

    foreach (var batch in batches)
    {
        var results = queryAttributeSet.Execute<T>(batch);
        rv.AddRange(results);
    }

    return rv;
}

我的尝试:

public IEnumerable<T> GetAttributes<T>(IEnumerable<Point> pts) where T : class, IPointResults, new()
{
    return GetAttributesHelper<T>(pts);
}

public IEnumerable<T> GetAttributes<T>(IEnumerable<Seg> segments) where T : class, ISegResults, new()
{
    return GetAttributesHelper<T>(segments);
}

public IEnumerable<T> GetAttributesHelper<T>(IEnumerable<PointSeg> x) where T : class, new()
{
    var queryAttributeSet = new QAS();

    var rv = new List<T>();
    var batches = x.Batch(25);

    Log.Debug($"Attempting to get {total} records using {batches.Count()} requests");

    foreach (var batch in batches)
    {
        var results = queryAttributeSet.Execute<T>(batch);
        rv.AddRange(results);
    }

    return rv;
}

我尝试使用PointSeg关注界面IPointSeg,但不幸的是,这不起作用,因为QAS.Execute不知道如何接受{{1}并且它无法修改。

然后我想如果我能以某种方式告诉代表使用哪个函数:

IPointSeg

这不起作用,因为您无法在Func&lt;&gt;中使用界面作为向函数发送2种类型的Func&lt;&gt;的方法。

我只想在使用我编写的批处理逻辑包装功能时使用QAS.Execute而不是重复

0 个答案:

没有答案