首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将源接口类型转换为目标接口类型

无法将源接口类型转换为目标接口类型
EN

Stack Overflow用户
提问于 2012-03-14 22:24:35
回答 2查看 1.4K关注 0票数 2

我想要处理文件,因为它们保存在四个目录中,它们都以非常不同的方式处理。

我有一个通用的处理器IFileQueueService,它在调度定时器上运行,加载所有的文件名,并为每个文件调用一个自定义的处理器IExecutionProcessor。这可能是一个简单的问题,但我的处理器具有比标准IExecutionProvider更多的属性,并且我不确定如何调用这些自定义处理器。

代码语言:javascript
复制
//Basic processor interface
public interface IExecutionProvider
{
    void ProcessFile(string file);
}

//Simplified version of one of the custom processor interfaces
public interface IKyoExecutionProcessor
{
    string DestinationPath { get; set; }
}

public class KyoExecutionProcessor : IExecutionProvider, IKyoExecutionProcessor
{
   //This processor moves a file to the DestinationPath.
}

public interface IFileQueueService
{
    string SourcePath { get; set; }
    IExecutionProvider ExecutionProvider { get; set; }
    void Start();
    void Stop();
}

public class FileProcessor : IFileQueueService
{
    ...
    public virtual void ProcessFileQueue()
    {
        IEnumerable<string> filesToProcess = GetFilesReadyToProcess();
        foreach (string file in filesToProcess.ToList())
        {
            ExecutionProvider.ProcessFile(file);
        }
    }
}

public class KYOFileSysWatcher : ServiceBase
{
    private IFileQueueService Processor { get; set; }
    private IKyoExecutionProcessor KyoCustomProcessor { get; set; }

    public KYOFileSysWatcher()
    {
        Processor = ObjectFactory.GetInstance<IFileQueueService>();
        KyoCustomProcessor = ObjectFactory.GetInstance<IKyoExecutionProcessor>();

        //This doesn't work, cannot convert source type to target type, but it implements the IExecutionProvider interface??? How do I do this?
        Processor.ExecutionProvider = KyoCustomProcessor;

        Processor.Start(); //Sets up dispatch timer
    }

 }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-14 22:41:28

泛型可以帮助你...

代码语言:javascript
复制
//Basic processor interface
public interface IExecutionProvider
{
    void ProcessFile(string file);
}

//Simplified version of one of the custom processor interfaces
public interface IKyoExecutionProcessor
{
    string DestinationPath { get; set; }
}

public interface IFileQueueService<TProvider>
    where TProvider : IExecutionProvider
{
    string SourcePath { get; set; }
    TProvider ExecutionProvider { get; set; }
    void Start();
    void Stop();
}

public class FileProcessor<TProvider> : IFileQueueService<TProvider>
    where TProvider : IExecutionProvider
{
    string[] GetFilesReadyToProcess() { return new string[0]; }
    public TProvider ExecutionProvider { get; set; }

    public virtual void ProcessFileQueue()
    {
        IEnumerable<string> filesToProcess = GetFilesReadyToProcess();
        foreach (string file in filesToProcess.ToList())
        {
            ExecutionProvider.ProcessFile(file);
        }
    }

    #region IFileQueueService<TProvider> Members

    public string SourcePath { get; set; }
    public void Start() { }
    public void Stop() { }

    #endregion
}

public class KyoExecutionProcessor : IExecutionProvider, IKyoExecutionProcessor
{
    //This processor moves a file to the DestinationPath.
    public string DestinationPath { get; set; }
    public void ProcessFile(string file) { }
}

public class ServiceBase<TProcessor>
    where TProcessor : IExecutionProvider
{
    protected FileProcessor<TProcessor> Processor { get; set; }
    protected TProcessor CustomProcessor { get; set; }
}

public class KYOFileSysWatcher : ServiceBase<KyoExecutionProcessor>
{
    public KYOFileSysWatcher()
    {
        Processor = ObjectFactory.GetInstance<FileProcessor<KyoExecutionProcessor>>();
        CustomProcessor = ObjectFactory.GetInstance<KyoExecutionProcessor>();

        //This compiles now!
        Processor.ExecutionProvider = CustomProcessor;

        Processor.Start(); //Sets up dispatch timer
    }
}
票数 1
EN

Stack Overflow用户

发布于 2012-03-14 22:29:20

您的IKyoExecutionProcessor接口应该派生自IExecutionProvider,如下所示:

代码语言:javascript
复制
public interface IKyoExecutionProcessor : IExecutionProvider 
{
    string DestinationPath { get; set; }
}

这确保了接口之间存在' is -a‘关系,这也将使您能够像这样实现KyoExecutionProcessor类:

代码语言:javascript
复制
public class KyoExecutionProcessor : IKyoExecutionProcessor
{
   public void ProcessFile(string file) { ... }
   public string DestinationPath { get; set; }
}

这样,任何实现IKyoExecutionProcessor的对象都可以被赋值给IExecutionProvider类型的变量。

代码语言:javascript
复制
Processor.ExecutionProvider = KyoCustomProcessor; // You can now appoint IKyoExecutionProcessor instance to IExecutionProvider variable
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9703831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档