Reputation: 367
I am trying to understand the following.
Say I have a class:
public sealed class UsageClass<TInstance> : IConsumer<TInstance>
where TInstance: Lazy<SomeClass>
Now say I want to inherit the lazyclass
public class ImplementationClass : SomeClass
Now when I try to do
UsageClass<ImplementationClass> instance = new UsageClass<ImplementationClass>();
I would get an error.
This leave me with a couple of options:
wrap TInstance in Lazy, which I do not want to do. I want the objects in the class to always be lazy.
Try and make Implementation class inherit Lazy which I do not think will work. Will the child class then be lazy? and I think the compiler will still complain.
So my question is. How do I successfully Inherit Lazy for use with generic classes given the constraints above?
Upvotes: 1
Views: 453
Reputation: 367
To those interested I solved my issue by instead of making my classes Lazy I instead marked all my method properties inside the interface as lazy. This way I could still make my items of T and have lazy instantiation down the chain.
Upvotes: 0
Reputation: 156968
You can't do that because it can't inherit the SomeClass
. TInstance
is statically defined to be Lazy<SomeClass>
, not Lazy<ImplementationClass>
. No generic there. You have to adjust your UsageClass
to make the Lazy
use the generic type instead to be statically typed (thanks to Dennis_E for giving the right pointer):
public class UsageClass<TInstance> : IConsumer<Lazy<TInstance>>
where TInstance : SomeClass
Then you can instantiate it like this:
UsageClass<ImplementationClass> instance = new UsageClass<ImplementationClass>();
Upvotes: 2