I Have a main dll Main.dll with 2 files MyBaseClass.cs and MyScript.cs both with namespace MainProject:
public class MyBaseClass
{
public string name ;
public MyBaseClass()
{
name = "base class" ;
}
}
Next i've made a second dll addOn.dll (using MainProject) with namespace SubProject
public class MySubClass : MyBaseClass
{
public MySubClass()
{
name = "sub class" ;
}
}
What i want to do is loading the second dll in MyScript, then instanciate MySubClass and cast it as MyBaseClass
public class MyScript
{
public static void init()
{
string myPath = ".../addOn.dll" ;
Assembly testAssembly = System.Reflection.Assembly.LoadFrom( myPath ) ;
//i retrieve types in external Assembly
Type[] myTypes = testAssembly.GetExportedTypes() ;
for(int i = 0; i < myTypes.Length; ++i)
{
//instanciate type => there is only MySubClass
System.Object testObj = testAssembly.CreateInstance( myTypes[i].ToString() ) ;
Debug.Log( testObj ) ; //=> print SubProject.MySubClass
Debug.Log(myTypes[i].BaseType ) ; //=>print MainProject.MyBaseClass
Debug.Log( myTypes[i].IsSubclassOf(typeof(MainProject.MyBaseClass))) ;
//=> print False. => It's seems that mySubClass doesn't derive from MyBaseClass anymore ?
//cast
MyBaseClass testCastObj = testObj as MyBaseClass;
Debug.Log( testCastObj ) ; //print null (cast fail)
//test if properties are well retrieved
PropertyInfo[] propList = testObj.GetType().GetProperties() ;
for(int j = 0; j < propList.Length; ++j)
{
Debug.Log( propList[j].Name.ToString() ) ; //print name, it's ok
}
}
}
}
So i succeed in instantiate my MySubClass, but without keeping heritage with MyBaseClass (or just my cast which is wrong... don't know) this is useless.
His anyone have the right way to do this?