adg icon indicating copy to clipboard operation
adg copied to clipboard

Polymorphism and casting object into actual type

Open akintos opened this issue 3 years ago • 0 comments

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?

akintos avatar Dec 14 '21 10:12 akintos