简体   繁体   English

接口/抽象类IEnumerable <T> T是特定的方法

[英]Interface/Abstract Class IEnumerable<T> Method Where T is Specific

I have a C# abstract class that I am using as a kind of interface 我有一个用作接口的C#抽象类

public abstract class ExcelParser
{
    protected FileInfo fileInfo { get; set; }

    protected bool FileIsValidExcelFile()...
    protected virtual string GetCellValue(int row, int column)...
    protected virtual int GetColumnLocation(string columnHeader)...

    public abstract IEnumerable<T> Parse();
}

My issue is with the abstract IEnumerable of T method, Parse(). 我的问题是T方法的抽象IEnumerable(即Parse())。

My problem is that I want the method to return an IEnumerable of a specific Type, but I don't care what that Type is at the abstract level. 我的问题是我希望该方法返回特定类型的IEnumerable,但是我不在乎该类型在抽象级别上是什么。 I only want the inheriting class to be specific about the IEnumerable returned. 我只希望继承的类特定于返回的IEnumerable。

Ignoring the fact that this doesn't compile, the other issue I'm running into is the execution in the presentation layer. 忽略不编译的事实,我遇到的另一个问题是表示层的执行。

        private string BuildSql(string fileName, bool minifySqlText, bool productCodeXref)
    {
        string result = string.Empty;
        ISqlBuilder sqlBuilder;
        ExcelParser excelParser;

        try
        {
            if (productCodeXref)
            {
                excelParser = new ProductCodeXrefExcelParser(fileName);
                var productCodeXrefs = excelParser.Parse();
                sqlBuilder = new ProductCodeXrefSqlBuilder(productCodeXrefs)
            }
            else
            {
                excelParser = new VendorExcelParser(fileName);
                var vendors = excelParser.Parse();
                sqlBuilder = new VendorSqlBuilder(vendors);
            }

            result = sqlBuilder.GetSql();
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message, "USER ERROR",
                MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
        }
        return result;
    }

This is a rough implementation in the presentation layer at the moment, but I don't know if this is going to work. 目前,这是表示层中的粗略实现,但我不知道这是否可行。 The reason I bring this up is because I had another implementation of ExcelParser that did compile, but that required me to be specific about what ExcelParser is eg.. 之所以提出这一点,是因为我有另一个可以编译的ExcelParser实现,但是这要求我具体说明ExcelParser是什么。

ExcelParser<Vendor>

... which completely defeats the purpose of doing it this way. ...完全违背了这样做的目的。

I know this because I attempted a similar solution to the following link, but then my class/interface required that I specify the type. 我知道这一点是因为我尝试了以下链接的类似解决方案,但随后我的类/接口要求我指定类型。 => How do i return IEnumerable<T> from a method => 如何从方法返回IEnumerable <T>

Is there any way to 1) Have a method in an abstract class or interface return an IEnumerable of T, but not care what that type is when it's being implemented, and 2) Insure that the interfaces/abstract classes don't care what type will be returned by the Parse method? 有什么办法1)在抽象类或接口中让方法返回IEnumerable T,但在实现时不关心该类型是什么,以及2)确保接口/抽象类不在乎什么类型将由Parse方法返回?

Is there any way to 有什么办法

1) Have a method in an abstract class or interface return an IEnumerable<T> , but not care what that type is when it's being implemented 1)在抽象类或接口中有一个方法返回IEnumerable<T> ,但不在乎该类型在实现时是什么类型

2) Insure that the interfaces/abstract classes don't care what type will be returned by the Parse method? 2)确保接口/抽象类不在乎Parse方法将返回哪种类型?

Yes - make the abstract class generic: 是的-使抽象类通用:

public abstract class ExcelParser<T>

The concrete class would then look like: 具体的类如下所示:

public class VendorExcelParser : ExcelParser<Vendor>

and would contain the logic that would create Vendor instances from the Excel data - utilizing the base abstract methods as much as possible. 并包含从Excel数据创建Vendor实例的逻辑-尽可能多地利用基本抽象方法。

You have to specify the type in some way for the type of T to be realized: it has to be either able to be implicitly determined (eg. by usage: Foo.Get<T>(T input), Foo.Get("") , or statically determined eg. Foo.Get<vendor>() 您必须以某种方式指定要实现T的类型的类型:必须能够隐式确定T的类型(例如,根据用法: Foo.Get<T>(T input), Foo.Get("")或静态确定,例如Foo.Get<vendor>()

If your presentation layer cannot know the type, then I'm at a loss as to how you intend to use a generic class to do so. 如果您的表示层不知道类型,那么我对您打算如何使用通用类做到这一点不知所措。 .Parse() needs to return a type in some manner. .Parse()需要以某种方式返回类型。 Possibly you can use an interface to accomplish the intended function here: if the objects returned will stick to a schema of sorts, then you don't have to care what the type is, just that it conforms to the interface. 可能您可以在此处使用接口来完成预期的功能:如果返回的对象将遵循某种模式,则不必关心类型是什么,只要它符合接口即可。

Depending on your usage, IEnumerable<object> may be appropriate? 根据您的用法, IEnumerable<object>可能合适吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模拟从IEnumerable <T>继承的抽象类 - Mocking an abstract class that inherits from IEnumerable<T> 其中T:IEnumerable <T> 方法约束 - where T : IEnumerable<T> method constraint C# IEnumerable 中的协变/逆变<t>其中 T 是通用类型,接口与 class</t> - C# Covariance / Contravariance in IEnumerable<T> where T is generic type, interface vs class 如何创建一个 List(of T),其中 T 是 (T) 的接口,List 的内容继承了一个实现 T 接口的抽象类 - How can I create a List(of T) where T is an interface of (T) and the contents of the List inherit an Abstract class that implements the interface of T 公共抽象类中实现内部接口的抽象方法不能编译? - Abstract method in public abstract class implementing an internal interface doesn't compile? IEnumerable <T> 和.Linq方法的行为在哪里? - IEnumerable<T> and .Where Linq method behaviour? 实现 IEnumerable<T> 界面 - Implementing IEnumerable<T> interface IEnumerable <T> 接口还是集合? - Is IEnumerable<T> an Interface or a Collection? 模糊的 IQueryable<T> .Where 和 IEnumerable<T> .Where 扩展方法 - Ambiguous IQueryable<T>.Where and IEnumerable<T>.Where on extension method 如何为实现IEnumerable的类实现GetEnumerator方法 <IEnumerable<T> &gt; - How to Implement GetEnumerator method for class that implements IEnumerable<IEnumerable<T>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM