2012-03-14 106 views
2

我想處理文件,因爲它們保存在四個目錄中,它們都以非常不同的方式處理。無法將源接口類型轉換爲目標接口類型

我有一個在調度計時器上運行的公共處理器IFileQueueService,加載所有文件名併爲每個文件調用自定義處理器IExecutionProcessor。這可能是一個簡單的問題,但我擁有比標準IExecutionProvider更多屬性的處理器,我不知道如何調用這些定製處理器。

//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 
    } 

} 
+0

如何'KyoCustomProcessor'執行'IExecutionProvider'?它明確是一個'IKyoExecutionProcessor'。 – 2012-03-14 14:29:55

回答

1

泛型救援...

//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 
    } 
} 
+0

我在見到你之前接受了第一個答案,但是非常感謝你。我會實現你的版本。 +1爲您的答案。 – strattonn 2012-03-14 15:19:23

+0

如果你願意,你可以改變接受的答案,我想。無論如何感謝+1。 – ja72 2012-03-14 15:27:30

1

IKyoExecutionProcessor接口應該從IExecutionProvider得出這樣的:

public interface IKyoExecutionProcessor : IExecutionProvider 
{ 
    string DestinationPath { get; set; } 
} 

這可以確保有一個「is-a的」接口之間的關係,這也將讓你實現KyoExecutionProcessor類像此:

public class KyoExecutionProcessor : IKyoExecutionProcessor 
{ 
    public void ProcessFile(string file) { ... } 
    public string DestinationPath { get; set; } 
} 

這種方式,實現IKyoExecutionProcessor任何對象可以被分配到類型的變量。

Processor.ExecutionProvider = KyoCustomProcessor; // You can now appoint IKyoExecutionProcessor instance to IExecutionProvider variable 
+0

完美。謝謝! – strattonn 2012-03-14 15:14:17

相關問題