Filling a basic ParentClass list with ChildClass "dynamically"

Hi,

Let me explain what I’m trying to do : I’m trying to create a custom inspector for an Item database which contains a list of items of different classes which all inherit from ParentClass. I want to be able to fill it with any ChildClass I could pass as a paramater, but keeping the attributes of the ChildClass. So if I cast my ChildClass as a ParentClass I cannot access the child attributes.

I have been able to get the Type of ChildClass from a string using a function which looks for the type in all current assemblies like this :

public Type getTypeFromAssembly(string className)
    {
        Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
        Type myType = null;
        foreach (Assembly assembly in assemblies)
        {
            myType = assembly.GetType(className);
            if (myType != null)
            {
                break;
            }
        }
        return myType;

The issue is when I want to add it to the list myParentClassList, I can’t cast the ChildClass using a Type variable like this :

string textType = "ResourceItem";
Type thisType = getTypeFromAssembly(textType);
object obj = Activator.CreateInstance(thisType);
myParentClassDatabase.myParentClassList.Add((thisType)obj);

And if I cast it as (ParentClass), attributes from the child class aren’t display since Unity can’t know which type of child it is.

I know the basics in C# but I’m no expert at all, so please bear with me :wink:
I’ve tried Convert.ChangeType but I’m running into the same problem.

Maybe there is a simple solution for what I’m trying to achieve ? Could anyone give me a hint ? Thanks !

The correct way to cast your instance to be able to add it to your list is this:

myParentClassDatabase.myParentClassList.Add((ParentClass)obj);

However keep in mind that this is completely pointless inside a custom inspector. Unity’s serialization system does not support inheritance for custom serializable classes. Make sure you read the page carefully. Fields in MonoBehaviours which have a custom serializable class as type are always serialized based on the variable type and not the actual type. Make sure you read the example in the section

No support for polymorphism

If you need inheritance and you want it to be serializable by Unity you have to derive your base class either from ScriptableObject or MonoBehaviour.

Actually there is a workaround for custom serializable classes, but it requires you to manually serialize / deserialize your custom classes with the aid of the ISerializationCallbackReceiver interface. Though in most cases it’s not worth to roll your own serialization system.

Hey Bunny, thank you for your answer. I was assuming this was possible since I think I saw people create Item Databes editors to fill their databases of items (weapon, armor, etc…).
So knowing what you just told me I assume they have just used a different list for each type, or define their type in an enum or something :wink:
I think I’m gonna end up using a type enum and one same class since I’m not going for something too complex, but I still think it’s sad we can’t do that.
Thanks again mate