I try to use a interface realization through a generic class. Wondering on some abstraction magic, but get compiler errors with this approach.
cannot convert from 'GClass<PosClass>' to 'GClass<IPos>'
Here a simplified example:
//Very simple generic with only data access, no internal manipulation
class GClass<T> {
T value;
public GClass(T value) {
this.value = value;
}
public T Get() {
return value;
}
}
//Simple interface
interface IPos {
int GetPos();
}
//Simple realization
class PosClass : IPos {
//Interface realization
public int GetPos() {
return 1;
}
public int GetAnotherImportantData() {
return -1;
}
}
class MainTrouble {
public int DoUsingInterface(GClass<IPos> interfaceableItem) {
//Do work using only interface methods
return interfaceableItem.Get().GetPos();
}
public int DoUsingInheritance() {
GClass<PosClass> item = new GClass<PosClass>(new PosClass());
//Next line error me
//cannot convert from 'GClass<PosClass>' to 'GClass<IPos>'
var r = DoUsingInterface(item);
//So, next I should execute a several methods using PosClass
return item.Get().GetAnotherImportantData();
}
}
Some suggestions? Maybe some tricky cast which I miss with it?