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
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 !