Issue with generic interfaces (covariance/contravariance)
Hello!
Currently, I have these interfaces within my project:
However, I'm unable to create a command like this:
... which makes it impossible for me to create a command and options dynamically. I have probably a design issue within my interfaces, so please, if there's anything that should be changed, don't hesitate to make these changes. Thanks! 😁
(Downcasting to
ICommand<object>
gives me an System.InvalidCastException
)1 Reply
Yeah, it does makes sense... I'll probably just need to rethink how these commands are created. Currently, I was looking to create a factory method that could create the "Command" and corresponding "Options" by a specific unique (string) name, but since I can't return something generic and always have to specify which sort of "TOptions" the command will use, the factory won't work.
Yeah, I actually did exactly that after my post, however, when I tried to cast it, it still gave me the exception.
I'll see if there's something weird with my implementation
Here's basically what I was trying to do:
But as you said, it won't work, since I can't return anything generic enough.
Do you happen to know why it doesn't work to cast to the interface?
Yeah, right, seems a bit unsafe.. 😄
However, I do have this abstract class:
I mean, I can't cast to that class either.. is it because it's an abstract class?
They do have relation
For example, this doesn't work:
Hmm, I see...
Yeah, either way, there's something missing from my design that I need to figure out 😄
Would this make sense @eXtensible Style Sheets?
Yeah, but I do need to register them somewhere.
Any suggestion for making it better? I'm eager to learn 🙂
I mean, I need a registry somewhere, of some sort.
All commands are of type "Command", this one:
However... now we are back at the casting problems:
How would I then specify which sort of options the command will use?
So basically, the "Execute" method will have no options parameter, but the class itself will have the options in a property?
Oh, I see!