Plug-In system for my application -> NullReferenceException

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello, developers!
Im making a application in c# wich I want to have plug-Ins.
I now have my main windows form application with the main form and 2 classes

Bot.cs <- Main form ScriptHelper.cs <- The file wich contains data needed for making the plugins
ScriptImplementation.cs <- This is a abstract class with has 4 abstract members

<ol>
abstract void Start() abstract ScriptImplemention getBot() abstract Bot bot { get; set; } abstract Boolean isRunning { get; set; } </ol>
For making the plug ins you need the scripthelper.cs so I referenced the Main Application to the class library project then made a simple working script implementing the abstract class ScriptImplemention.<br/>
<br/>
Now I want to have the dll to be loaded on runtime when you select it from a listview.
I used this code for it:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; private <span style="color:Blue; void startScriptToolStripMenuItem_Click(<span style="color:Blue; object sender, EventArgs e)
{
PlugIn plug = <span style="color:Blue; new PlugIn(<span style="color:Blue; this);
DialogResult dg = plug.ShowDialog(<span style="color:Blue; this);
<span style="color:Blue; if (dg != System.Windows.Forms.DialogResult.OK)
<span style="color:Blue; return;

<span style="color:Blue; object[] s = <span style="color:Blue; new <span style="color:Blue; object[] { };
Console.WriteLine(<span style="color:#A31515; "Loading dll…");
<span style="color:Blue; try
{
botScript = LoadDllMethod(<span style="color:#A31515; "BotScript.BotScript", <span style="color:#A31515; "getBot", selecteddll, s);
botScript.bot = <span style="color:Blue; this;
botScript.isRunning = <span style="color:Blue; true;
Console.WriteLine(<span style="color:#A31515; "Dll method called!");
}
<span style="color:Blue; catch (Exception ex)
{
Console.WriteLine(<span style="color:#A31515; "Error:" + ex.ToString());
}
Script = <span style="color:Blue; new System.Threading.Thread(<span style="color:Blue; new System.Threading.ThreadStart(RunBot));
Script.Priority = System.Threading.ThreadPriority.Highest;
Script.Name = <span style="color:#A31515; "Script Hook";
Script.Start();

}

<span style="color:Blue; static ScriptImplementation LoadDllMethod(<span style="color:Blue; string title_class, <span style="color:Blue; string title_void, <span style="color:Blue; string path, <span style="color:Blue; object[] parameters)
{
Assembly u = Assembly.LoadFile(path);
Type t = u.GetType(title_class);
<span style="color:Blue; if (t != <span style="color:Blue; null)
{
MethodInfo m = t.GetMethod(title_void);
<span style="color:Blue; if (m != <span style="color:Blue; null)
{
<span style="color:Blue; if (parameters.Length >= 1)
{
<span style="color:Blue; object[] myparam = <span style="color:Blue; new <span style="color:Blue; object[1];
myparam[0] = parameters;
<span style="color:Blue; return (ScriptImplementation)m.Invoke(<span style="color:Blue; null, myparam);
}
<span style="color:Blue; else
<span style="color:Blue; return (ScriptImplementation)m.Invoke(<span style="color:Blue; null, <span style="color:Blue; null);
}
}
Exception ex = <span style="color:Blue; new Exception(<span style="color:#A31515; "method/class not found");
<span style="color:Blue; throw ex;
}

<span style="color:Blue; private <span style="color:Blue; void RunBot()
{
RunBot(botScript); <span style="color:Green; // ERROR OCCURES HERE
<span style="color:Green; // NULLREFERENCE EXCEPTION WAS UNHANDLED
}

<span style="color:Blue; private <span style="color:Blue; void RunBot(ScriptImplementation bot)
{
bot.Start();
}
[/code]

If copied the contents of the plug in to the main project for testing if it works. And if I then typ

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; private <span style="color:Blue; void RunBot()
{
RunBot(<span style="color:Blue; new MyCustomBot);
}

<span style="color:Blue; private <span style="color:Blue; void RunBot(ScriptImplementation bot)
{
bot.Start();
}
[/code]

<br/>
Then it does work :s


Can somebody help? thanks! <hr class="sig Hope This Helps! <br/>
<br/>
If this post answers your question, please click <b>"Mark As Answer"</b>.<br/>
If this post is helpful please click <b>"Mark as Helpful"</b>.<br/>
Else you got to wait until a MVP or MCC replies they are the <b>"PROs"
</b><br/>

View the full article
 
Back
Top