Sign in to follow this  
Krum110487

Invoking Methods quickly from a dynamic DLL

Recommended Posts

Krum110487    110
EDIT:
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]

Share this post


Link to post
Share on other sites
Krum110487    110
I am only double-posting to show the solution to my problem!

What I had to end up doing was reference the dll containing my parent class from the script dll.

Then (and this is the part that had me confused) in the reference properties you MUST change "Copy Local" to false... and the following code works like a charm!


...

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();

}

}

}


I hope this helps someone developing a dll based engine!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this