Trying to get "Applets" to work (Like Contribute)

Discuss anything programming related.
Post Reply
User avatar
Click16
Posts: 1941
Joined: Mon Dec 31, 2007 4:36 am
Location: United States

Trying to get "Applets" to work (Like Contribute)

Post by Click16 »

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:

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();
        }
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.
Image
User avatar
OwnZ joO
Posts: 1197
Joined: Sun Dec 09, 2007 4:46 pm

Re: Trying to get "Applets" to work (Like Contribute)

Post by OwnZ joO »

The var keyword just allows the compiler to infer the type you're using, but from all the code before it in the method it can gather no information about the actual type being returned, so it will just be an object since that's what it returns. Is type returned by asm.GetType or obj null? Obviously whatever is returned by Activator.CreateInstance is not a Beta_Applet.AppletUI or a subclass of it. Judging by the above code it should be though, so I'm thinking the assembly either doesn't have a AppletUI or maybe mistyped Beta_Applet.AppleUI or left out part of the namespace or something.

Definitely debug the code and make sure that type is not null, and maybe even do a Type objType = obj.GetType() to find out what type the object returned actually is.
User avatar
Click16
Posts: 1941
Joined: Mon Dec 31, 2007 4:36 am
Location: United States

Re: Trying to get "Applets" to work (Like Contribute)

Post by Click16 »

OwnZ joO wrote:The var keyword just allows the compiler to infer the type you're using, but from all the code before it in the method it can gather no information about the actual type being returned, so it will just be an object since that's what it returns. Is type returned by asm.GetType or obj null? Obviously whatever is returned by Activator.CreateInstance is not a Beta_Applet.AppletUI or a subclass of it. Judging by the above code it should be though, so I'm thinking the assembly either doesn't have a AppletUI or maybe mistyped Beta_Applet.AppleUI or left out part of the namespace or something.

Definitely debug the code and make sure that type is not null, and maybe even do a Type objType = obj.GetType() to find out what type the object returned actually is.
I can tell you that it does in fact work, I just can't cast obj to Beta_Applet.AppletUI class. I debugged and checked obj. It has all of the properties from Beta_Applet.AppletUI class. I can cast it to a Control and UserControl and both work. It just won't be able to do anything since I can't access the Map property or OnTagSelect() method that is native to Beta_Applet.AppletUI.

EDIT: Here look at these images.
Image
-The design of the test applet

Image
-The code paused. You can see how obj has the Map property, and the label reference.

Image
- The applet passed as a UserControl, added to the collapsable Panel and added to the main form.
Image
User avatar
OwnZ joO
Posts: 1197
Joined: Sun Dec 09, 2007 4:46 pm

Re: Trying to get "Applets" to work (Like Contribute)

Post by OwnZ joO »

Wierd... It does look like it is of the right type according to the debugger. Try:

Code: Select all

Beta_Applet.AppletUI applet = obj as Beta_Applet.AppletUI;
They are of the exact same type correct? Not just have all the same properties and derive from two separate but identical AppletUI classes right?
User avatar
Click16
Posts: 1941
Joined: Mon Dec 31, 2007 4:36 am
Location: United States

Re: Trying to get "Applets" to work (Like Contribute)

Post by Click16 »

OwnZ joO wrote:Wierd... It does look like it is of the right type according to the debugger. Try:

Code: Select all

Beta_Applet.AppletUI applet = obj as Beta_Applet.AppletUI;
They are of the exact same type correct? Not just have all the same properties and derive from two separate but identical AppletUI classes right?
Yeah it is the class, and stting obj as Beta_Applet.AppletUI causes obj to be null.
Image
User avatar
OwnZ joO
Posts: 1197
Joined: Sun Dec 09, 2007 4:46 pm

Re: Trying to get "Applets" to work (Like Contribute)

Post by OwnZ joO »

Well the code

Code: Select all

MyClass mc = obj as MyClass;
translates to

Code: Select all

MyClass mc = obj != null && obj is MyClass ? (MyClass)obj : null;
So it's either null(which the debugger says it's not, or it's not the right type(although it looks to be according to the debugger). It could be a bug I guess, but I doubt that.
Post Reply