recente ho affrontato questo problem..I rendo conto che è una vecchia questione, ma pensato che qualcuno potrebbe trovare utile la soluzione che ho trovato (usando net 4.0). Questa è la configurazione che ho avuto:
public interface ISomeInterface{
String GetEntityType{ get; }
}
public abstract class BaseClass : ISomeInterface{
public String GetEntityType {
get{ return this.GetType().ToString(); }
}
}
public class ChildA : BaseClass { }
public class ChildB : BaseClass { }
Quello che ho fatto è stato creare un metodo di fabbrica:
public static dynamic GetRealType { ISomeInterface param } {
return Activator.CreateInstance("dllname", param.GetEntityType);
}
Creare l'oggetto come un tipo dinamico è stato quello che ha risolto per me. Ho dedotto il tipo da avere un metodo:
public void DoSomething<T>(T param){
IList<T> list = somecode...
}
questo mi ha permesso di fare una chiamata al metodo e lasciare .net dedurre il tipo
public void myMethod(ISomeInterface param) {
dynamic value = Factory.GetRealType (param);
DoSomething(value);
}
speranza non ho fatto tutto confuso e in realtà aiuta, anche, mi spiace per il muro di testo
fonte
2012-01-18 00:49:52
IList è un'interfaccia, quindi non si può nuova esso. È necessario un nuovo tipo che implementa IList come Elenco . –