EditorService.cs

Oct 8, 2009 at 4:46 AM

May I make the following suggested changes to EditorService.cs:

FIRST

GetEditor(PropertyItem, PropertyGridLabel) should be re-written as follows. Notice the commented out line is replaced by the new line of code underneath it.

public static ValueEditorBase GetEditor(PropertyItem propertyItem, PropertyGridLabel label)
{
 if (propertyItem == null) throw new ArgumentNullException("propertyItem");
 EditorAttribute attribute = propertyItem.GetAttribute<EditorAttribute>();
 if (attribute != null)
 {
  Type editorType = Type.GetType(attribute.EditorTypeName, false);
        if (editorType != null)
        {
            //return Activator.CreateInstance(editorType) as ValueEditorBase;
            return Activator.CreateInstance(editorType, label, propertyItem) as ValueEditorBase;
        }
 }
 Type propertyType = propertyItem.PropertyType;
 ValueEditorBase editor = GetEditor(propertyType, label, propertyItem);
 while (editor == null && propertyType.BaseType != null)
 {
  propertyType = propertyType.BaseType;
  editor = GetEditor(propertyType, label, propertyItem);
 }
 return editor;
}

The reason I suggest this change is that I don't see any value in creating a custom editor using the constructor with no parameters. If you do this, your editor will never know the value you want to edit--so before my change you would always be editing "null". Maybe I am missing something?

SECOND

It strikes me as very odd and unnecessarily confusing to have the following method signatures:

public static ValueEditorBase GetEditor(PropertyItem propertyItem, PropertyGridLabel label)
public static ValueEditorBase GetEditor(Type propertyType, PropertyGridLabel label, PropertyItem property)

It would seem to me that it would make more sense for the re-ordering of GetEditor to look like the following as this matches the ValueEditor base constructor signature.

public static ValueEditorBase GetEditor(PropertyGridLabel label, PropertyItem propertyItem)

This might seem like a small request, but it actually screwed me up because I was expecting consistent signatures.

Coordinator
Oct 9, 2009 at 3:01 AM

Thanks for the feedback. All good points and I agree. I've actually enhanced this greatly since my last release for the things you pointed out and more, but is part of another project I'm working on. I had to essential re-create the missing sections of the component model thats excluded from the .NET Silverlight CLR. So now it uses things like PropertyGridEntry, PropertyDescriptor etc. similar to System.Windows.Forms. Again appreciate the feedback. Hopefully Silverlight 4 might have the additional portions available in a separate Assembly. I know they want to keep the install size small so I figure an optional Assembly could be released via the Toolkit or something.

 

 

Coordinator
Oct 9, 2009 at 3:17 AM
I felt like I kind of left the last post unfinished. So even though I can't release the library, I provided some snippets of the new sections.
New constructor for the Editor base class...
public PropertyGridEditor(SolariumPropertyDescriptor descriptor)
{
	if (null == descriptor)
		throw new ArgumentNullException("descriptor");
	this.Descriptor = descriptor;
}
New Editor resolver...
public object GetEditor()
{
	object editor = GetEditorFromType(this.PropertyType, this);

	if (null == editor)
	{
		#region Get Editor from Attribute
		EditorAttribute attribute = this.GetAttribute<EditorAttribute>();
		if (attribute != null)
		{
			Type editorBaseType = Type.GetType(attribute.EditorBaseTypeName, false);
			if (null != editorBaseType)
			{
				Type editorType = Type.GetType(attribute.EditorTypeName, false);
				if (null != editorType && editorBaseType.IsAssignableFrom(editorType))
				{
					editors.Add(this.PropertyType, editorBaseType.FullName);
					editor = GetEditorFromType(this.PropertyType, this);
				}
			}
		}
		#endregion
	}

	if (null == editor)
	{
		#region If no editor attribute, try BaseType
		Type propertyType = this.PropertyType;
		while (editor == null && propertyType.BaseType != null)
		{
			propertyType = propertyType.BaseType;
			editor = GetEditorFromType(propertyType, this);
		}
		#endregion
	}

	return editor;
}
New Factory...
private static object GetEditorFromType(Type propertyType, SolariumPropertyDescriptor descriptor)
{
	object editor = null;
	string editorName;

	editors.TryGetValue(propertyType, out editorName);
	if (string.IsNullOrEmpty(editorName))
		return editor;

	Type editorType = Type.GetType(editorName, false);
	if (null != editorType)
		editor = Activator.CreateInstance(editorType, descriptor);

	return editor;
}