c# Invalid Cast Exception when using an interface as generic type (Unity)
So I am not sure if this is a C# generics issue I don't understand or
whether it has something to do with the Unity compiler not being able to
handle it. I come from C++ and am slightly experienced with c# but not
with generics.
So onto the issue: I have a generic class called TurnOrderQueue. in short
it is a custom Queue that orders things based on some values that are
passed in with it. (It is in a namespace called TurnOrderQueueNS)
I have an empty(for now) interface called IActor which is to represent
things that can take actions in a turn based game.
Finally I have a TurnOrderManager that handles everything with turn order
and owns a TurnOrderQueue.
When I attempt to instantiate the TurnOrderQueue using IActor as the type
I get an invalid cast exception public
TurnOrderQueueNS.TurnOrderQueue<IActor> TurnOrder = new
TurnOrderQueueNS.TurnOrderQueue<IActor>();
The Exception I get at runtime is: InvalidCastException: Cannot cast from
source type to destination type.
TurnOrderQueueNS.TurnOrderQueue`1[IActor]..ctor () TurnManager..ctor ()
Can you not use Interfaces as the type with generics in C# or am i missing
something?
No comments:
Post a Comment