Return 通过接口 c# 从不同类型的列表中得到结果

Return results from lists of different type via interface c#

我创建了一个接口,理论上应该能够 return 多个不同类型的通用列表,以便为客户提供各种信息。当我尝试循环遍历列表的结果时,它只能 return 第一个集合,你能帮我理解我应该如何 returning 来自以下结果:

接口class:

public interface IExampleInterface{}

public class ExampleType : IExampleInterface
{
    public int First;
    public int Last;
}

public class ExampleAmount : IExampleInterface
{
    public decimal Amount;
    public decimal TotalFee;
}

public class ExampleFacts : IExampleInterface
{
    public bool TooLow;
    public bool TooHigh;
}

接口提供商:

public class ExampleInterfaceProvider
{
    private static readonly string conn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
    public static List<IExampleInterface> ExampleResults(int id) 
    {
        //declare variables, read from database query using ExecuteReader...
        var sT = new ExampleType
        {
            First = first;
            Last = last;
        }
        var sA = new ExampleAmount
        {
            Amount = amount;
            TotalFee = totalFee;
        }
        var sF = new ExampleFacts
        {
            TooHigh = tooHigh;
            TooLow = tooLow;
        }
        var exampleResults = new List<IExampleInterface> {sT, sA, sF};
        return exampleResults;
    }
}

在页面上我需要return数据:

foreach (dynamic item in ExampleResults(0))
{
    Response.Write(item.First.ToString())
    Response.Write(item.Last.ToString())
    //The first two for 'sT' read fine, it breaks here
    Response.Write(item.Amount.ToString())
    //... And so on
}

如有任何帮助,我们将不胜感激,

谢谢

我想,除了比较实现,没有别的办法了;

        foreach (IExampleInterface item in ExampleResults(0))
        {
            if (item is ExampleType)
            {
                var exampleType = (ExampleType)item;
                Response.Write(exampleType.First.ToString())
                Response.Write(exampleType.Last.ToString())
            }
            else if (item is ExampleAmount)
            {
                var exampleAmount = (ExampleAmount)item;
                Response.Write(exampleAmount.Amount.ToString())
          }
            //... And so on
        }

如果您使用的是 C# 7,则可以按 switch case

执行
        foreach (IExampleInterface item in ExampleResults(0))
        {
            switch (item)
            {
                case ExampleType c:
                    Response.Write(c.First.ToString());
                    Response.Write(c.Last.ToString());
                    break;
                case ExampleAmount c:
                    Response.Write(c.Amount.ToString());
                    break;
                default:
                    break;
            }
            //... And so on
        }

您可以找到 documentation.

所以基本上,实现 IExampleInterface 的项目都应该以某种特定于实现接口的实际类型的方式写入 Response

那这个怎么样:

public interface IExampleInterface
{
    void WriteTo(Response response);
}

public class ExampleType : IExampleInterface
{
    public int First;
    public int Last;

    public void WriteTo(Response response)
    {
        response.Write(First.ToString());
        response.Write(Last.ToString());
    }
}

public class ExampleAmount : IExampleInterface
{
    public decimal Amount;
    public decimal TotalFee;

    public void WriteTo(Response response)
    {
        response.Write(Amount.ToString());
        response.Write(TotalFee.ToString());
    }
}

public class ExampleFacts : IExampleInterface
{
    public bool TooLow;
    public bool TooHigh;

    public void WriteTo(Response response)
    {
        response.Write(TooLow.ToString());
        response.Write(TooHigh.ToString());
    }
}

然后:

foreach (IExampleInterface item in ExampleResults(0))
{
    item.WriteTo(Response);
}

假设 Response 是一个包含响应实例的变量而不是静态变量 class。