View previous topic :: View next topic |
Author |
Message |
Baaleos
Joined: 02 Sep 2007 Posts: 830
|
Posted: Mon Nov 07, 2011 12:38 Post subject: How to make a nwnx style plugin application - C# |
|
|
Pretty much, im a somewhat alright C# developer.
However, my next development project, I want to impliment a nwnx style plugin system, with a dll being loaded at startup, and being able to accept commands from the parent app.
I already have the code that allows me to load dll's at runtime via a
Reflection/resolve assembly call.
My guess is that I just change
'Assembly.Load(StreamToBytes(input))' to be the file path for the stream.
eg - for each assembly in the directory, load it.
I was just wondering if this sounds like a plan of action for how to go about creating a similar interface system.
1. Ok, I guess I can create an instance of the dll assembly/interface, and then store it in a Hashtable - so I can access it later. (associative array)
2. The DLL will need to have a common function across the platform.
eg a Method call within it called 'ReceiveRequest(string[]);'
3. So, the parent app, will take the note of the receiving plugin to send the command to, and the args, and then access the plugin instance from the hashtable - by name, and feed the arguments into the 'ReceiveRequest()' function.
4. The Plugin would accept the arguements and handle the code.
Does this sound feasible?
Or is there a better way of doing it? |
|
Back to top |
|
|
virusman
Joined: 30 Jan 2005 Posts: 1020 Location: Russia
|
|
Back to top |
|
|
Baaleos
Joined: 02 Sep 2007 Posts: 830
|
Posted: Mon Nov 07, 2011 15:06 Post subject: |
|
|
Hi Virusman,
Yeah, C# definitely would make it object orientated.
I was just wondering if anyone had insight into the way to pass information from Host to Plugin.
eg - Host would Scan directory
Gets *.dll files
Loads each one as an assembly, and then tries to instantiate the dll's. <-- Somehow?
eg im thinking I could create a base class type that is common in the host, and plugins.
eg
Code: |
public class Plugin()
{
public void Load()
{
}
public string Request(object[] args)
{
}
}
|
Now, my mind gets fuzzy around some bits.
1. I have 5 dll's
2. I am looping through them
3. I load each assembly
4. How to I create an instance of the class within that assembly, programatically.
eg- To create an instance, I would need to do
ClassType obj = new ClassType();
eg- With a constructor.
How do I create an instance of an object, if I dont know what the object is.
Once I have created and stored the reference to the object, I should be grand.
Im gonna study this
http://www.codeproject.com/KB/cs/c__plugin_architecture.aspx
Hopfully this will help me out. |
|
Back to top |
|
|
virusman
Joined: 30 Jan 2005 Posts: 1020 Location: Russia
|
Posted: Mon Nov 07, 2011 15:26 Post subject: |
|
|
Define interface in the base app, implement it in the plugins.
Btw, a good example of C# plugin system is NWN2 Toolset plugin API.
This code will enumerate all classes in the assembly and instantiate only the classes that implement the interface:
Code: | Assembly as = Assembly.LoadFile(FileName);
foreach (Type type in as.GetTypes())
{
if (type.BaseType == typeof(IPlugin))
{
someArray[i] = (IPlugin)Activator.CreateInstance(type);
}
}
|
_________________ In Soviet Russia, NWN plays you!
Last edited by virusman on Mon Nov 07, 2011 15:38; edited 1 time in total |
|
Back to top |
|
|
Baaleos
Joined: 02 Sep 2007 Posts: 830
|
Posted: Mon Nov 07, 2011 15:36 Post subject: thx |
|
|
Thanks,
I saw that same code - but didnt understand why/what it meant.
It makes perfect sense now the way you have explained it. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|