Wednesday, November 09, 2005

Full and Partial Assembly Names

Preface

Recently my wife encountered weird problem with PropertyGrid control. As you know this control displays object properties and gives the posibility to edit them. If object has properties that return CollectionBase types, property grid displays nice dialog that allows editing collection items.

Type of the edit dialog is specified by the Editor attribute. ( see MSDN for details ).

The Problem

Property called Items returns CollectionBase type. Namely StringCollection.
The snippet below illustrates the situation:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design",
"System.Drawing.Design.UITypeEditor, System.Drawing")]
public StringCollection Items
{
//common stuff goes here
}

Everything looks fine, however when the object with this property is given to PropertyGrid, collection edit
dialog doesn't recognize its true string nature. Instead of StringCollectionEditor we observer common editor dialog,
where collection items have type System.Object. Somehow PropertyGrid didn't recognized the attribute, or attribute
contents...

Stephen Toub in his blog directly addresses
this problem. Yes the problem was with partial names. So the solution is very simple if you're the owner of the
code, where that attribute is specified, and you can easily chage it.
So, the working version will look like:

[Editor(
"System.Windows.Forms.Design.StringCollectionEditor, System.Design, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Drawing.Design.UITypeEditor, System.Drawing, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public StringCollection Items
{
//common stuff goes here
}

For those who use 3-d party classes and have no access to source code, there are 2 solutions:
1) modify the DEVENV environment variable
2) put System.Design.dll assembly to the folder, where your app resides

First way isn't good as it may affect other applications that rely on that variable and it is not safe.

My wife chose second way, she just added System.Design.dll to the references list and set "Copy local" property to true.

No comments:

Post a Comment