最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

c# - "Could not load type 'System.Object' from assembly 'System.Private.CoreLib' becaus

matteradmin3PV0评论

Context

I'm creating a Visual Studio 2022 Extension with a VSIX Project that consists of a window that displays the current Form (WinForms) classes of your project in a ComboBox and when you select one, it loads all their controls in a ListView so you can edit each Z-Order index (which determines which control is under or on top of the others), something you normally can't do.

To do this, I thought of "reading" the assembly of the current project with Reflection and retrieve the Form classes. For the ComboBox, I actually just identify the Form classes by their extension (.Designer.cs) but to actually load the controls of each Form class, I have to load its assembly, but I'm struggling to do so because it's giving me a lot of dependency errors.

At first, it was 'System.Windows.Forms' and a few other dependencies but I was able to solve it using the AssemblyResolve event by adding every required assembly from the Framework.

Main Issue

However, when I try to retrieve the type from the assembly, the following error appears:

Could not load type 'System.Object' from assembly 'System.Private.CoreLib' because the parent does not exist.

I've looked everywhere, but I couldn't find a fix for this error in a case similar to mine. This is the first time I'm making a Visual Studio Extension and using assemblies like this so I would be very grateful if anyone could help me out, thanks in advance!

If you need more info, the project is published in GitHub, the error exactly happens at the line 82 of the ZOrderToolWindowControl.xaml.cs file, inside the FormsComboBox_selectionChanged event, that looks like this:

private void FormsComboBox_SelectionChanged(object sender, RoutedEventArgs e)
        {
            string file = "";
            try
            {
                // Reflection Aproach - Loads all the requested assemblies
                AppDomain.CurrentDomain.AssemblyResolve += Resolver;

                // Dinamicly Load Project's Assembly
                Assembly assembly = Assembly.LoadFrom(Path.Combine(Path.GetDirectoryName(clientProject.FullName), "bin\\Debug\\net8.0-windows\\" + clientProject.Properties.Item("OutputFilename").Value.ToString()));

                // Get the assembly name (with namespaces)
                file = files.Where(s => s.Contains(FormsComboBox.SelectedItem.ToString())).First();

                // Get the Type from the assembly (HERE IS WHERE THE ERROR POPS UP)
                Type formType = assembly.GetType(GetAssemblyName(file), true, true);

                // Create an object of the Type
                object formClass = Activator.CreateInstance(formType);

                // Fill the ListView with the project's Form Class Controls
                controls.Clear();
                if (formClass is System.Windows.Forms.Form form)
                {
                    foreach (System.Windows.Forms.Control control in form.Controls)
                    {
                        controls.Add(control);
                    }
                }

                ControlListView.Items.Clear();
                for (int i = 0; i < controls.Count; i++)
                {
                    ControlListView.Items.Add("(" + controls[i].GetType().Name + ") " + controls[i].Name);
                }
            } catch (Exception ex) {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }

Also, for some reason, sometimes it happens on the next line where it tries to instantiate an object. You can also just add a comment and I'll try to answer as soon as I can.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far