Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DesignerAttribute usage

I am developing a winforms application, and I want to place the designers into a separate project, because it requires the reference to .Net full framework (System.Design), while the application itself requires .Net framework client version only.

I have 3 projects:

  1. TestControls with TestUserControl
  2. TestControls.Designers with PanelHodelerDesigner
  3. TestApp to test the TestUserControl

TestControls and TestControlsDesigners are signed and registered in GAC.

When I define Designer for the user control as follows, VS cannot locate the designer

[Designer("TestControls.Designers.PanelHolderDesigner, TestControls.Designers")]
public partial class TestUserControl : UserControl, IPanelHolder
{
    ...
}

When I change it to fully qualified name including version, culture and publicKeyToken like follows, VS loads the designer correctly.

[Designer("TestControls.Designers.PanelHolderDesigner, TestControls.Designers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=520302431ebc763b")]
public partial class TestUserControl : UserControl, IPanelHolder
{
    ...
}

All samples in the web that I found teach that I should use shorten version. Why it does not work for me? What else should I do to use the short version of designer attribute?

like image 1000
xll Avatar asked May 10 '26 07:05

xll


1 Answers

Feature, not a bug. The GAC was designed to be able to store multiple versions of an assembly side-by-side. If you don't specify the [AsssemblyVersion] then the CLR doesn't stand a chance to guess at the correct one. So it doesn't try, it only looks in the probing path of the running app for the display name. These "samples" you found probably all assume that you don't use the GAC.

It is probably going to be difficult to talk you out of this, but separating the designer from the control isn't a great idea. Microsoft does this, but they are extraordinarily reluctant to ever change the [AssemblyVersion] of a .NET Framework assembly. And expend massive resources to ensure they never have to. That's is very hard to duplicate, versioning assemblies is rather important to mere mortals like us, we don't spend three or more years on designing and testing assemblies to ensure that the hundreds of thousands of programmers that use them don't run into trouble.

Short from having trouble on your own machine, getting this to still work correctly on another programmer's machine is hard enough in itself. I never hesitate to use typeof() in the [Designer] attribute and include the designer with the control. Never a surprise that way. That's a spit of IL that never gets loaded at runtime, taking advantage of the you-don't-pay-for-what-you-don't-use CLR behavior is hard to pass up when it solves icky problems like this.

like image 113
Hans Passant Avatar answered May 11 '26 21:05

Hans Passant