SAF(NET) = STEPHEN A. FUQUA operating on the Web since 1995

Stephen is a web developer, Bahá'í, and interfaith activist in St. Paul, Minnesota. He likes to write about religion, social justice, sustainability, science, programming, &c.

July 17, 2007

Visual Studio Says: "Failed to create component"

Problem: "Failed to create component" error pops up when dragging a custom Windows Forms control from the Toolbox onto a form. Offending line: foreach (Attribute att in Assembly.GetEntryAssembly().GetCustomAttributes(true)).

Background: I have started building a custom DLL containing items for re-use between different Windows Forms projects. An obvious candidate is an About window. I created this as a custom control in the DLL project; the control contains a textbox, which I want to fill with the application name, the version, copyright information, and company name. All of this is to come from the AssemblyInfo.cs file, using System.Reflection where necessary:

string copyright = string.Empty;

foreach (Attribute att in Assembly.GetEntryAssembly().GetCustomAttributes(true))
{
     if (att is AssemblyCopyrightAttribute)
          copyright = (att as AssemblyCopyrightAttribute).Copyright;
}

string txt = Application.ProductName + "\n"
               + "Version " + Application.ProductVersion + "\n"
               + copyright + "\n"
               + Application.CompanyName;

Solution: First, I comment out the foreach block to see if I can now drop the user control into the form. Rebuild dll, re-drop user control… voíla! But this is interesting looking…

about1.jpg

Apparently the Reflection works even in Visual Studio rather well ;-) — but the assembly doesn't have embedded custom attributes. So now that I have this in the form, let's try uncommenting the offending lines so that I can again see the copyright information. Close the form, re-open… and again I get an error, though this time it is simply "object reference not set to an instance of an object."

Let us run this application and see if it works, despite the problem with loading it. The answer is yes! (Screenshot not included).

Lesson: don't be surprised if Reflection tells you something about Visual Studio itself. Work around any errors and trust that it will work properly once your application is running (that is, assuming you worked around in such a way that you can still compile).

deprecated

On safnet.com

Other sites managed or developed by S.A.F.

S.A.F. elsewhere on the web

  • LinkedIn
    LinkedIn can actually be useful when looking for prospective hires and business or organizational partners
  • GoodReads
    A fun and relatively-unknown social networking site geared towards one's book list
  • Live Journal
    Mirror of the blog at safnet.com, so that a few LJ friends can more easily read and comment there