2014-01-08 25 views
1

我已經有點難以解決如何將數據對象傳遞給它的處理器類。我試圖給出一個下面的問題的簡單例子。我試圖找出是否有一種類型安全的方式來實例化使用通用接口的處理器?創建通用接口實例時可以動態指定類型嗎?

乾杯,

查理

class APieceOfState 
    { 
     public string AbitOfData { get; set; } 
    } 

interface IDescribeSomething<in T> 
    { 
     void Process(T type) ; 
    } 


class ImplementWhatIsDescribed : IDescribeSomething<APieceOfState> 
    { 
     public void Process(APieceOfState type) 
     { 
      Console.WriteLine("Processing {0}", type.GetType()); 
     } 

    } 

private static void Main(string[] args) 
    { 
     var nextTest = new ImplementWhatIsDescribed(); 
     var newStateObj = new APieceOfState(); 
     nextTest.Process(newStateObj); 

     // Map processor to data in a dictionary 
     var dic = new Dictionary<Type, Type>(); 
     var task = new APieceOfState(); 
     var taskProcessor = new ImplementWhatIsDescribed(); 
     dic.Add(task.GetType(), taskProcessor.GetType()); 

     // Lookup processor using data type 
     Type lookupProcessorType; 
     dic.TryGetValue(task.GetType(), out lookupProcessorType); 

     //         vvvvvvvvvvv - How can I make this dynamic based on task.GetType() ? 
     var instance = (IDescribeSomething<APieceOfState>)Activator.CreateInstance(lookupProcessorType); 
     instance.Process(task); 

     Console.ReadKey(); 
    } 

回答

0

第一種選擇 - 你可以使用dynamic(不是強類型)

dynamic instance = Activator.CreateInstance(lookupProcessorType); 
instance.Process(task); 

如果您需要編譯時檢查你不得不去使用通用接口不通用接口或通用方法。原因是IFoo<OneType>IFoo<OtherType>是不相關的繼承,所以沒有共同的靜態類型,你可以參考兩個。

樣品用於非通用接口:

interface IDescribeSomething 
{ 
    void Process(SomeBaseType type); 
} 


IDescribeSomething instance = 
    (IDescribeSomething)Activator.CreateInstance(lookupProcessorType); 
instance.Process(task); 

樣品爲類似於IEnumrable<T>通用+基。請注意,一般不解決您的問題,但至少有一些其他的地方可以有通用代碼:

interface IDescribeSomething 
{ 
    void Process(SomeBaseType type); 
} 

interface IDescribeSomething<T> : IDescribeSomething 
{ 
    void Process(T type); 
} 

class APieceOfState : SomeBaseType {} 

class ImplementWhatIsDescribed : IDescribeSomething<APieceOfState> 
{ 
    public void Process(SomeBaseType type) 
    { 
     Process((APieceOfState)type); 
    } 
    public void Process(APieceOfState type) 
    { 
     Console.WriteLine("Processing {0}", type.GetType()); 
    } 

} 

IDescribeSomething instance = 
    (IDescribeSomething)Activator.CreateInstance(lookupProcessorType); 
instance.Process(task); 

// but in this case you can sometime write strongly type one too 
// if you got strongly typed version of interface 
IDescribeSomething<APieceOfState> p =... 
p.Process(task) 

強類型的通用方法的樣品

void DoProcess<T>(T task) 
{ 
    IDescribeSomething<T> instance = 
    (IDescribeSomething<T>)Activator.CreateInstance(lookupProcessorType); 
    instance.Process(task); 

} 

您可以通過使用MakeGenricMethod調用該方法 - How do I use reflection to call a generic method?

0

你要這樣呢?

public void Process<T>(T type) where T : ICommon 
{ 
    Console.WriteLine("Processing {0}", type.GetType()); 
} 

public interface ICommon 
{ 

} 
相關問題