It seems I have found my problem, although I still can't seem to fix it:
http://www.yoda.arachsys.com/csharp/plugin.html
--------------------------------------------------------------------
First off, hello!
I am developing a simple 2D engine for PC/Xbox 360 for a game, but mostly for practice with C#, XNA, Classes, and Game making!
I want my engine to use DLL's for the scripting engine I am creating, I can load the script and run the code, but since I load the DLL dynamically, I can't run the methods without using:
object obj = type.InvokeMember(null, BindingFlags.Public..., null, null, null); type.InvokeMember("TestFunction", BindingFlags.InvokeMethod, null, obj, null); break;
I plan on putting all of my "map" scripts into one DLL, so I don't have a thousand DLL's to take care of, which will look something like this...
namespace Script{ public class Map0001 : Map { public void TestFunction() { Console.WriteLine("DLL CALL WORKS!"); } } public class Map0002 : Map { public void TestFunction() { Console.WriteLine("DLL CALL WORKS!"); } }}
I can successfully find the class I want, but it is within a sequential loop, which is fine for loading the game, but I would like to deep copy the class to prevent me having to search for it over and over.
I am not exactly sure of how to do that with a dynamically loaded DLL, it shouldn't be too hard, I just can't seem to find the solution!
I would prefer if I didn't have to put a clone function within every class.
My code so far:
...assembly = Assembly.LoadFile(...);foreach (Type type in assembly.GetTypes()){ if(type.IsClass && type.Namespace == "Script") { Console.WriteLine(type.name); MethodInfo method = type.GetMethod("TestFunction", new Type[0]); if(method != null) { object obj = type.InvokeMember(null, BindingFlags.Public..., null, null, null); type.InvokeMember("TestFunction", BindingFlags.InvokeMethod, null, obj, null); break; } }
any help would be awesome! thanks!
--------------------------------------------------------------------------
EDIT:
I think I found what I want to do, but I keep getting a runtime error.
...assembly = Assembly.LoadFile(...);foreach (Type type in assembly.GetTypes()){ if(type.IsClass && type.Namespace == "Script") { Console.WriteLine(type.name); Engine.Map MapTemp = (Engine.Map) Activator.CreateInstance(type); MapTemp.onLoad(); } }}
DLL:
namespace Script{ public class Map0001 : Map { public void TestFunction() { Console.WriteLine(" - DLL CALL WORKS!"); } } public class Map0002 : Map { public void TestFunction() { Console.WriteLine(" - DLL CALL WORKS!"); } }}
Map Class:
namespace Script{ public class Map { public virtual void onLoad() { Console.WriteLine(" - Map Class (Parent Class)"); } }}
I am getting an InvalidCastException here:
Engine.Map MapTemp = (Engine.Map) Activator.CreateInstance(type);
My guess is that it is treating the Map class within the DLL as a separate class from the one I am using...although they are identical... I can't seem to make this work either!
[Edited by - Krum110487 on October 13, 2010 3:29:06 PM]