简体   繁体   English

C#泛型-知道在抽象方法中使用哪个派生类

[英]C# Generics - Know which derived class to use in an Abstract Method

I am working on a C# project to parse files of different kinds. 我正在研究一个C#项目,以分析各种文件。 In order to do this, I have created the following kind of class structure: 为此,我创建了以下类型的类结构:

interface FileType {}
    class FileType1 : FileType {}
    class FileType2 : FileType {}

abstract class FileProcessor {}
    class Processor_FileType1 : FileProcessor {} // Will use FileType1 - type of storage class
    class Processor_FileType2 : FileProcessor {} // Will use FileType2 - type of storage class

So, since each FileProcessor uses a different kind of FileType , I was hoping to write some kind of method in my Base FileProcessor class to be able to get the values from the file looking something along the lines of: 因此,由于每个FileProcessor使用不同类型的FileType ,因此我希望在Base FileProcessor类中编写某种方法,以便能够从文件中获取值,如下所示:

abstract class FileProcessor 
{
    protected List<T> getValuesFromFile<T>() where T:FileType
    {
        try
        {
            otherClass.doProcess<T>();
        }
        catch (Exception ex)
        {
            throw new Exception("Unable to retrieve the data from the file.", ex);
        }
    }
}

And, in another library I've been using (related to parsing Excel files), which I cannot change, I have the following kind of method: 而且,在我一直使用的(与解析Excel文件有关的)另一个库中,我无法更改它,它具有以下方法:

        public List<T> doProcess<T>() where T : class, new()
        {
            // the actual work
        }

But I am getting an error in my getValuesFromFile method stating that The type 'T' must be a reference Type to be able to return a List in my method. 但是我在getValuesFromFile方法中遇到错误,指出The type 'T' must be a reference Type ,才能在我的方法中返回列表。

I am trying to figure out how to do this to minimize having to write the code for pulling the data from the file into each of the separate derived processor classes. 我试图弄清楚如何做到这一点,以减少将代码从文件中提取到每个单独的派生处理器类中的代码。

Is there any way to do this or is this just bad programming for Generics? 有没有办法做到这一点,或者这只是泛型的不良编程?

Your otherClass.doProcess() method is declared like 您的otherClass.doProcess()方法的声明方式如下

public List<T> doProcess<T>() where T : class, new()

So this requires T to be a reference type and to have a default parameterless constructor . 因此,这要求T引用类型并具有默认的无参数构造函数

In the calling method you have only restricted T to implement the FileType interface: 在调用方法中,您仅限制了T来实现FileType接口:

List<T> getValuesFromFile<T>() where T:FileType

This isn't enough. 这还不够。 An interface could also be implemented by a value type and it doesn't say anything about constructors. 接口也可以通过值类型来实现,它没有说明构造函数。 So you'd have to change the constraints to: 因此,您必须将约束更改为:

List<T> getValuesFromFile<T>() where T: class, FileType, new()

(Note that the class constraint has to be the first in a constraint declaration). (请注意, class约束必须是约束声明中的第一个)。

You can ensure that T is a reference type by constraining as: 您可以通过限制为T来确保T是引用类型:

where T : class, FileType

It's hard for me to understand exactly what you're trying to do, so I can't provide guidance on your use of generics more generally. 我很难确切地了解您要执行的操作,因此无法为您更一般地使用泛型提供指导。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM