C# .NET - Activator.CreateInstance casting error

Asked By Gerard Gleeson on 17-May-08 05:10 PM
Im trying to write a application to extract files from archives.On startup it goes through all dlls in the base directory and finds plugins each intended to handle a different type of archive (.tar .rar...).Its been done dynamically for the usuall plugin reasons.What qualafies as a plugin is a public class which extends the abstract class CoreArchive.Archive.I thought I had worked out how to do this when working with a particular dll but when I apply a simple foreach loop cycling though each dll the process failed.It would find all dlls and invoke the correct constructor but then casting would fail.The parameters been passed appear to be the same.When I added in one line before the loop which created a 1 length array the process will work again .So im guessing the problem lies with calling the static method Assembley.LoadFrom(dll) , it alters something if called more than once.Though I don't know how.i have a feeling its something low level like different assemblies, different types therefore although appering the same the computer views them as different hence casting fails.But I dont know  and more importantly I cant fix it.

Exception gives:
"Unable to cast object of type 'ArchiveType.Tar.TarArchive' to type 'CoreArchive.Archive'."

ArchiveType.Tar.TarArchive extends CoreArchive.Archive so casting should not fail....

public static void initialize() { string appPath = AppDomain.CurrentDomain.BaseDirectory; //Temporary path for debugging purposes appPath = "C:\\hdrive\\C#\\Projects\\ArchiveExtractor\\TarArchive\\bin\\Debug\\"; string[] dlls = System.IO.Directory.GetFiles(appPath, "*.dll",SearchOption.TopDirectoryOnly); Type baseArchive = typeof(Archive); *** //dlls = new string[1] { appPath + "TarArchive.dll" }; //If the above line is uncommented code runs fine going through all the control statements and casting is ok foreach( string dll in dlls ) { { *** Assembly ass = Assembly.LoadFrom(dll); //This declaration of Assembley here more than once appers to be causing the error futher on foreach( Type type in ass.GetTypes() ) { if( type.IsClass && type.IsPublic && type.BaseType.FullName.Equals(baseArchive.FullName) ) { try { *** Archive arc = (Archive) Activator.CreateInstance(type); //Archive arc = (Archive) ass.CreateInstance(type.FullName); //This is where the exception is thrown from both above lines were atempted with the same results // //Exception: //{"Unable to cast object of type 'ArchiveType.Tar.TarArchive' to type 'CoreArchive.Archive'."} System.Exception {System.InvalidCastException} // ArchiveType.Tar.TarArchive is the class thats loaded from TarArchive.dll //When the first marked line is uncommented this class casts ok if( arc.GetType().IsSubclassOf(baseArchive) ) { Archive[] tmp = new Archive[archives.Length + 1]; for( int i = 0; i < archives.Length; i++ ) tmp[i] = archives[i]; tmp[tmp.Length - 1] = arc; archives = tmp; } } catch( Exception e ) { } } } } catch( Exception e ) { } } initializedArchives = true; } Thanks Ger

I think this - Peter Bromberg replied to Gerard Gleeson on 17-May-08 05:50 PM

"ArchiveType.Tar.TarArchive extends CoreArchive.Archive so casting should not fail...."

   may be a presumption that is not necessarily accurate. How about using an interface, say ICoreArchive. Then if each type implements the interface you would have no problem.

Solution found - Gerard Gleeson replied to Peter Bromberg on 18-May-08 07:13 AM

Hey thanks for the reply but I managed to find the solution all I had to do was change

Assembly.LoadFrom(dll); to Assembly.LoadFile(dll);