How to serialize/deserialize a class without knowing its type

Ok so my goal is to replicate the modability of Kerbal Space Program. In that game each part is defined by a config file that lists all the PartModuals and their variables (PartModuals being scripts extended from their PartModual script). Whats more is that as long as you extend from their PartModual class, you can add your own custom scripts and then add them to parts in the same way in the configs.

I have been trying to do this in XML. If I specifically name the class:

var AClassIcanSave : ThatClass = ThatClass();

I can serialize that, however if I try to use a child of that class it throws an error telling me the class I am trying to save is not a primitive:

class BetterClass extends ThatClass
{
   var betterFactor : float = 10;
}


var AClassIcanSave : ThatClass = BetterClass();

I also tried:

var AClassIcanSave = BetterClass();

and

    class BetterClass extends ScriptableObject
    {
       var betterFactor : float = 10;
    }
    
    
    var AClassIcanSave : ScriptableObject = BetterClass();

Which also fail with the same error.

How can I serialize a class without specifically referencing it so that I can save and load any class that my potential modders could create?

Figured it out!

The Load Script:

#pragma strict
import System.Xml;
import System.Xml.Serialization;
import System.Collections.Generic;
//   Import all the stuff we need


var LoadObject : String;  //Name of the thing we want to load


//Main Load Function
function Load()
{
	var serializer : XmlSerializer = new XmlSerializer(SerialObject);
	var stream : Stream = new FileStream(Application.dataPath + "/StreamingAssets/" + LoadObject + ".xml", FileMode.Open);
	var ObjToLoad : SerialObject = serializer.Deserialize(stream) as SerialObject;
	stream.Close();
	
	
	var NewObject : GameObject = Resources.Load("1x2Brick") as GameObject;
	NewObject = Instantiate(NewObject, new Vector3(0, 0, 0), Quaternion.identity);
	NewObject.name = ObjToLoad.SerialName;
	
	
	for(var i = 0; i < ObjToLoad.scripts.Count;)
	{

		var NewScript = NewObject.AddComponent(ObjToLoad.scripts*.SerialName);*

_ for(var j = 0; j < ObjToLoad.scripts*.SerialVars.Count;)_
_
{_
_ var NewVar : SerialVar = ObjToLoad.scripts.SerialVars[j];
NewScript.GetType().GetField(NewVar.SerialName).SetValue(NewScript, NewVar.Value);
j++;
}*_

* i++;*
* }*

}

// Data Classes

public class SerialObject
{
* @XmlAttribute(“Name”)*
* public var SerialName : String;*

* @XmlArray(“ClassList”)*
* @XmlArrayItem(“Script”)*
* public var scripts : List. = new List.();*
}

public class SerialScript
{
* @XmlAttribute(“Name”)*
* public var SerialName : String;*

* @XmlArray(“Variables”)*
* @XmlArrayItem(“var”)*
* public var SerialVars : List. = new List.();*
}

public class SerialVar
{
* @XmlAttribute(“Name”)*
* public var SerialName: String;*

* public var Value;*
}
The Config it is loading (saved as MudBrick.xml) :
<?xml version="1.0" encoding="Windows-1252"?>





The script the Config is referencing:
#pragma strict

public var Speed : float;

public var Rot : float;

function Start ()
{

}

function Update ()
{
* transform.position += Vector3(Speed,0,0);*
* transform.rotation = Quaternion.EulerAngles(0,Rot,0);*
}
So what this is doing is the load function is given the name of the xml file I want to load. The first variable in the xml file is the name of the script. This is easily used to create an instance of that script by name with the gameobject.addcomponent(“nameofscript”) function. This allows for easy loading of monobehaviors.
The next part is more tricky. Using reflection the line “NewScript.GetType().GetField(NewVar.SerialName).SetValue(NewScript, NewVar.Value);” references public variables in the newly instance monobehavior and set them based on the values found in the xml. That said reflection is slow so my plan is to create a list of gameobjects using the xml on initial load and then I can instance them from the list as needed at runtime.
Thanks everyone for the nudge in the right direction. Hope this helps anyone trying to do a similar task.