- Home /
Question by
AngryCSharpNerd · Jan 17, 2014 at 11:11 PM ·
classclassestypetypeof
Get Superclass of Subclass in Unityscript
I've been looking all over. Using typeof() will only return the subclass.
class Enemy{}
class Beast extends Enemy{}
var beast = new Beast();
print(beast.GetType() == typeof(Enemy));
// >false
For my game to be as efficient as possible, I really need to somehow find the superclass of a subclass, and this is really annoying me. How can you get the superclass?
Comment
Best Answer
Answer by JoaoOliveira · Jan 17, 2014 at 11:19 PM
public class A {}
public class B : A {}
public static void Main()
{
Console.WriteLine(typeof(A) == typeof(B)); // false
Console.WriteLine(typeof(A).IsAssignableFrom(typeof(B))); // true
Console.WriteLine(typeof(B).IsSubclassOf(typeof(A))); // true
}
As found here (btw, a much better place for language related questions).
Your answer
Follow this Question
Related Questions
class design preference 1 Answer
[C#]how to use child class type in parent class method. 1 Answer
Public class used in another class (C#) 1 Answer
C# : comparison of type between gameobject of specific type 3 Answers
Creating a class? 2 Answers