knah / Il2CppAssemblyUnhollower

A tool to generate Managed->IL2CPP proxy assemblies
GNU Lesser General Public License v3.0
499 stars 88 forks source link

Polymorphism and casting object into actual type #78

Open akintos opened 2 years ago

akintos commented 2 years ago

For example,

class A : Il2CppSystem.Object { }
class B : A { }
class C : A { }

var list = new List<A>() { new A(), new B(), new C() };

In this case, when I try to get list[2].GetType(), I get typeof(A). I was able to get actual type of it using code below

public static Type GetActualType(Il2CppSystem.Object obj)
{
    var cpptype = obj.GetIl2CppType();
    return Type.GetType(cpptype.AssemblyQualifiedName);
}

but I cannot cast object into type with cppobj.Cast<actualType>() because I cannot provide type variable into it. Is there any other way to cast il2cpp object into specific type variable in runtime?