H
HC
I've stripped down my code to barebones to demonstrate (below).
I have interfaces and classes which implement them. I force my generic
collection to accept only the interface type. If I create an instance
of the generic class using the class implemention of the interface I
find it can't be converted to the interface version, and vice versa.
This a pain with my current design.
Can someone please advise on what best practice might be in this
situation. Are there any cardinal rules or similar that I'm violating,
passing an interface to the generic collection instead of an actual
class?
The compiler should know they both support the same interface so why
not compatible?
thanks
interface IMyInterface
{
}
class ImplementsMyInterface : IMyInterface
{
}
class GenericClass<T> where T : IMyInterface
{
}
class TestAbove
{
void MethodWhichTakesClass(GenericClass<ImplementsMyInterface> ts)
{
MethodWhichTakesInterface(ts); // Cannot convert type
'Test.GenericClass<Test.ImplementsMyInterface>' to
'Test.GenericClass<Test.IMyInterface>'
}
void MethodWhichTakesInterface(GenericClass<IMyInterface> ts)
{
MethodWhichTakesClass(ts); // cannot convert from
'Test.GenericClass<Test.IMyInterface>' to
'Test.GenericClass<Test.ImplementsMyInterface>'
}
}
I have interfaces and classes which implement them. I force my generic
collection to accept only the interface type. If I create an instance
of the generic class using the class implemention of the interface I
find it can't be converted to the interface version, and vice versa.
This a pain with my current design.
Can someone please advise on what best practice might be in this
situation. Are there any cardinal rules or similar that I'm violating,
passing an interface to the generic collection instead of an actual
class?
The compiler should know they both support the same interface so why
not compatible?
thanks
interface IMyInterface
{
}
class ImplementsMyInterface : IMyInterface
{
}
class GenericClass<T> where T : IMyInterface
{
}
class TestAbove
{
void MethodWhichTakesClass(GenericClass<ImplementsMyInterface> ts)
{
MethodWhichTakesInterface(ts); // Cannot convert type
'Test.GenericClass<Test.ImplementsMyInterface>' to
'Test.GenericClass<Test.IMyInterface>'
}
void MethodWhichTakesInterface(GenericClass<IMyInterface> ts)
{
MethodWhichTakesClass(ts); // cannot convert from
'Test.GenericClass<Test.IMyInterface>' to
'Test.GenericClass<Test.ImplementsMyInterface>'
}
}