I fell into trap 22 and seem to be unable to find a way out. I am trying to implement a simple [Service Locator] [1] represented by the GetInstance method in the following code example. Now the problem is that I get a compiler error in the return statement:
Cannot implicitly convert type 'Cyberspace.SubClass' to 'Cyberspace.BaseClass<T>'
I tried rewriting, so SubClass is a generic class, but then I get a compiler error in the return statement in the DoSomething method.
Is it possible to compile this code while preserving the principles of the Service Locator template that allow me to have an abstract return type in the GetInstace method? Or am I trying to achieve something impossible here?
namespace Cyberspace { class BaseClass<T> { BaseClass<T> GetInstance() { return new SubClass(); } virtual T DoSomething() { return default(T); } } class SubClass : BaseClass<OtherClass> { public override OtherClass DoSomething() { var other = new OtherClass { Description = "Generics are hard"}; return other; } } class OtherClass { internal string Description { get; set; } } }
Attempt 2:
namespace Cyberspace { class BaseClass<T> { static BaseClass<T> GetInstance() // The "Service Locator" method { return new SubClass<T>(); } internal virtual T DoSomething() { return default(T); } } class SubClass<T> : BaseClass<T> where T: OtherClass { internal override T DoSomething() { var other = new OtherClass { Description = "Generics are hard"}; return (T) other; } } class OtherClass { internal string Description { get; set; } } }
This results in the following error on the line return new SubClass<T>();
The type 'T' cannot be used as type parameter 'T' in the generic type or method 'Cyberspace.SubClass<T>'. There is no boxing conversion or type parameter conversion from 'T' to 'Cyberspace.OtherClass'
source share