Trying to get "Applets" to work (Like Contribute)
Posted: Sat Dec 31, 2011 3:54 am
Hey all. I am remaking Beta in hope that I can keep code much more organized using Applet style editors. There is one "applet" window when the program loads, this is the File explorer. It isn't an applet loaded from a DLL file, its just the control inside the form. When a map is loaded, I want to load all the applets into control forms, using a template that contains it's own instance of Halo2BMap, for editing the library based on your own purpose, it also contains a UserControl named "AppletUI.cs" where you will put the design and UI of the applet itself. Anyways the problem is that I can retrieve the UserControl AppletUI.cs, but not the actual class it is using, even with a direct reference. The reason I need this is so that when the applets are loading (upon opening of the map) It is unable to cast to the class that the user control is inherited by. So here is my code:
now, If I try to set var to (Beta_Applet.AppletUI)Activator.CreateInstance(type); it throws an InvalidCast Exception. What should I do? If you need to know, I need var to be an AppletUI because I need to tell it when the user clicks on a tag or how to load / reload / close a map.
Code: Select all
private void LoadApplets()
{
//Load Applets
string[] AppletPaths = System.IO.Directory.GetDirectories(Application.StartupPath + "\\Applets");
for (int i = 0; i < AppletPaths.Length; i++)
{
//Check for File
string AppletLocation = AppletPaths[i] + "\\Beta Applet.dll";
if (File.Exists(AppletLocation))
{
//Get Name
string AppletName = AppletPaths[i].Replace(Application.StartupPath + "\\Applets\\", "");
//Get Assembly
Assembly asm = Assembly.LoadFile(AppletLocation);
Type type = asm.GetType("Beta_Applet.AppletUI");
var obj = Activator.CreateInstance(type);
CollapsablePanel CP = new CollapsablePanel();
CP.Text = AppletName;
((Control)obj).Dock = DockStyle.Fill;
CP.Controls.Add((Control)obj);
CP.Size = new Size(4 + ((Control)obj).Width, 22 + ((Control)obj).Height);
this.Controls.Add(CP);
}
}
ReloadWindowsMenuItem();
}