I have the following code:
kAIDataPort<T> lOtherEndCast = ( kAIDataPort<T>)lOtherEnd;
This throws the following exception:
[A]kAI.Core.kAIDataPort`1[UnityEngine.GameObject] cannot be cast to
[B]kAI.Core.kAIDataPort`1[UnityEngine.GameObject]. Type A originates from 'kAICore...
(The exception is shortened for readability, but there is no difference between A and B.
All other questions on generic casting, apparently, relate to lists and inherited types, but here we have only an object of a certain type and it can not pass it to this type.
Not looking for work, I use a non-core base class with a non-typed method to accomplish what I need to do, I just want to understand why this throws an exception.
This is in .NET 3.5 (using Unity, which still does not support .NET 4 ...)
Full exception:
[A]kAI.Core.kAIDataPort`1[UnityEngine.GameObject] cannot be cast to
[B]kAI.Core.kAIDataPort`1[UnityEngine.GameObject].
Type A originates from 'kAICore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'E:\dev\C#\kAI\kAI-Editor\bin\Debug\kAICore.dll'.
Type B originates from 'kAICore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'E:\dev\C#\kAI\kAI-Editor\bin\Debug\kAICore.dll'.
Update:
The problem was loading the Unity library twice, but it was loading the same DLL (but never unloaded). Code for this:
FileStream lDLLStream = lDllPath.GetFile().OpenRead();
byte[] lDLLArray = new byte[lDLLStream.Length];
lDLLStream.Read(lDLLArray, 0, (int)lDLLStream.Length);
lDLLStream.Close();
Assembly lLoadedAssembly = Assembly.Load(lDLLArray);
lLoadedAssembly.GetExportedTypes();
return lLoadedAssembly;
, , dll ?
, DLL , :
this.GetType().Equals(lOtherEnd.GetType()) false
:
typeof(T).Equals(lOtherEnd.GetType().GenericTypeArguments[0]) true