C#我可以实现与IEnumerable对象和结构的接口吗?

C#我可以实现与IEnumerable对象和结构的接口吗?,c#,struct,interface,C#,Struct,Interface,所以我想写一个接口,它应该能够用任何数据实现。这是我到现在为止写的界面。我选择IEnumerable的原因是因为我需要给类Computer或struct Processor public interface IData<T> where T : IEnumerable<object> { public T ReadData(); public void WriteData(T list); } 我试着这样做,但似乎因为拳击我不被允许这样做 public

所以我想写一个接口,它应该能够用任何数据实现。这是我到现在为止写的界面。我选择IEnumerable的原因是因为我需要给类Computer或struct Processor

 public interface IData<T> where T : IEnumerable<object>
{
    public T ReadData();
    public void WriteData(T list);
}
我试着这样做,但似乎因为拳击我不被允许这样做

public class ProcessorServiceCSV : IData<IEnumerable<Processor>>
{   private string Path { get; set; }
    private FileMode Filemode { get; set; }
    public ProcessorServiceCSV(string path, FileMode fileMode)
    {
        Path = path;
        Filemode = fileMode;
    }
    //reads Processor list from CSV file
    public IEnumerable<Processor> ReadData()
    {
        try
        {
            using (var reader = new StreamReader(Path))
            using (var csv = new CsvReader(reader))
            {
                csv.Configuration.CultureInfo = CultureInfo.InvariantCulture;
                csv.Configuration.Delimiter = ",";
                csv.Configuration.RegisterClassMap<ProcessorMap>();
                var records = csv.GetRecords<Processor>().ToList();

                return records.ToList();
            }
        }
        catch (FileNotFoundException)
        {
            throw new DataCustomException("File not found", this);
        }
        catch (Exception e)
        {
            throw new DataCustomException("Something's wrong happened:" + e.Message, this);
        }
    } public void WriteData(IEnumerable<Processor> processors)
    {
        try
        {
            using (var stream = File.Open(Path, Filemode))
            using (StreamWriter sw = new StreamWriter(stream))
            using (CsvWriter cw = new CsvWriter(sw))
            {
                foreach (Processor processor in processors)
                {
                    cw.Configuration.RegisterClassMap<ProcessorMap>();
                    cw.WriteRecord<Processor>(processor);
                    cw.NextRecord();
                }
            }
        }
        catch (FileNotFoundException)
        {
            throw new DataCustomException("File not found", this);
        }
        catch (FileLoadException)
        {
            throw new DataCustomException("File could not be opened", this);
        }
        catch (Exception e)
        {
            throw new DataCustomException("Something's wrong happened:" + e.Message, this);
        }
    }
}
公共类ProcessorServiceCSV:IData
{私有字符串路径{get;set;}
私有文件模式文件模式{get;set;}
公共处理器ServiceCSV(字符串路径、文件模式、文件模式)
{
路径=路径;
Filemode=Filemode;
}
//从CSV文件读取处理器列表
公共IEnumerable ReadData()
{
尝试
{
使用(变量读取器=新的流读取器(路径))
使用(var csv=新的CsvReader(读卡器))
{
csv.Configuration.CultureInfo=CultureInfo.InvariantCulture;
csv.Configuration.Delimiter=“,”;
csv.Configuration.RegisterClassMap();
var records=csv.GetRecords().ToList();
返回记录。ToList();
}
}
捕获(FileNotFoundException)
{
抛出新的DataCustomException(“未找到文件”,这是);
}
捕获(例外e)
{
抛出新的DataCustomException(“发生了错误:+e.Message,this”);
}
}public void WriteData(IEnumerable处理器)
{
尝试
{
使用(var stream=File.Open(Path,Filemode))
使用(StreamWriter sw=新StreamWriter(流))
使用(CsvWriter cw=新CsvWriter(sw))
{
foreach(处理器中的处理器)
{
cw.Configuration.RegisterClassMap();
连续写入记录(处理器);
cw.NextRecord();
}
}
}
捕获(FileNotFoundException)
{
抛出新的DataCustomException(“未找到文件”,这是);
}
捕获(FileLoadException)
{
抛出新的DataCustomException(“无法打开文件”,此错误);
}
捕获(例外e)
{
抛出新的DataCustomException(“发生了错误:+e.Message,this”);
}
}
}
}


我知道我可以将处理器从结构更改为类,但是否可以保留结构?提前感谢您

您还有很多其他问题,包括没有给我们提供完整的工作代码

但是,如果您使用
处理器
结构的接口而不是实际的结构类型,那么您应该能够完成您想要做的事情

另外,请注意我是如何在您的类中更改
T
的类型的。在
t
约束中不需要
IEnumerable
。我确实删除了您的一些代码以使其能够正常工作(例如,struct构造函数中的异常),因此您需要在这里做更多的工作

公共接口IData
{
IEnumerable ReadData();
无效写入数据(IEnumerable list);
}
公共接口IP处理器{
字符串名称{get;set;}
字符串AmazonLink{get;set;}
字符串AmazonBin{get;set;}
}
公共结构处理器:IPProcessor
{
公共字符串名称{get;set;}
公共字符串AmazonLink{get;set;}
公共字符串AmazonBin{get;set;}
公共处理器(字符串名称、字符串链接)
{
名称=名称;
//构造函数解析在URLGenerator项目中生成AmazonURL所需的元素
AmazonLink=link.Substring(0,link.IndexOf(“&dc”);
字符串binStart=link.Substring(link.IndexOf(“bin%”)加4);
AmazonBin=“%7C”+binStart.Substring(2);
}
}
公共类ProcessorServiceCSV:IData其中T:IPProcessor
{私有字符串路径{get;set;}
私有文件模式文件模式{get;set;}
公共处理器ServiceCSV(字符串路径、文件模式、文件模式)
{
路径=路径;
Filemode=Filemode;
}
//从CSV文件读取处理器列表
公共IEnumerable ReadData()
{
尝试
{
使用(变量读取器=新的流读取器(路径))
使用(var csv=新的CsvReader(读卡器))
{
csv.Configuration.CultureInfo=CultureInfo.InvariantCulture;
csv.Configuration.Delimiter=“,”;
csv.Configuration.RegisterClassMap();
var records=csv.GetRecords().ToList();
返回记录。ToList();
}
}
捕获(FileNotFoundException)
{
抛出新的DataCustomException(“未找到文件”,这是);
}
捕获(例外e)
{
抛出新的DataCustomException(“发生了错误:+e.Message,this”);
}
}
}

这是您尝试执行的基本框架代码吗?请注意,泛型集合是
IEnumerable
而不是
IEnumetable
,因此我更新了您的
IData
定义

public class Computer
{
}

public struct Processor
{
}

public interface IData<T> 
{
    IEnumerable<T> ReadData();
    void WriteData(IEnumerable<T> list);
}


public class ComputerData : IData<Computer>
{
    public IEnumerable<Computer> ReadData()
    {
        throw new NotImplementedException();
    }

    public void WriteData(IEnumerable<Computer> list)
    {
        throw new NotImplementedException();
    }
}
public class ProcessorData : IData<Processor>
{
    public IEnumerable<Processor> ReadData()
    {
        throw new NotImplementedException();
    }

    public void WriteData(IEnumerable<Processor> list)
    {
        throw new NotImplementedException();
    }
}
公共类计算机
{
}
公共结构处理器
{
}
公共接口IData
{
IEnumerable ReadData();
无效写入数据(IEnumerable list);
}
公共类计算机数据:IData
{
公共IEnumerable ReadData()
{
抛出新的NotImplementedException();
}
公共无效写入数据(IEnumerable列表)
{
抛出新的NotImplementedException();
}
}
公共类进程数据:IData
{
公共IEnumerable ReadData()
{
抛出新的NotImplementedException();
}
公共无效写入数据(IEnumerable列表)
{
抛出新的NotImplementedException();
}
}

请说明此代码是否满足您的要求,如果不满足,请说明原因。

是的,我知道我可以这样做泛型,但问题是我希望与类计算机和结构处理器使用相同的接口。所以
public class Computer
{
}

public struct Processor
{
}

public interface IData<T> 
{
    IEnumerable<T> ReadData();
    void WriteData(IEnumerable<T> list);
}


public class ComputerData : IData<Computer>
{
    public IEnumerable<Computer> ReadData()
    {
        throw new NotImplementedException();
    }

    public void WriteData(IEnumerable<Computer> list)
    {
        throw new NotImplementedException();
    }
}
public class ProcessorData : IData<Processor>
{
    public IEnumerable<Processor> ReadData()
    {
        throw new NotImplementedException();
    }

    public void WriteData(IEnumerable<Processor> list)
    {
        throw new NotImplementedException();
    }
}