private void OnNewLinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { InstanceCreationEditor ice = e.Link.LinkData as InstanceCreationEditor; Debug.Assert(ice != null, "How do we have a link without the InstanceCreationEditor?"); if (ice != null && gridView?.SelectedGridEntry != null) { Type createType = gridView.SelectedGridEntry.PropertyType; if (createType != null) { gridView.CloseDropDown(); object newValue = ice.CreateInstance(gridView.SelectedGridEntry, createType); if (newValue != null) { // make sure we got what we asked for. // if (!createType.IsInstanceOfType(newValue)) { throw new InvalidCastException(string.Format(SR.PropertyGridViewEditorCreatedInvalidObject, createType)); } gridView.CommitValue(newValue); } } } }
/// <summary> /// Extends CreateInstance so that methods that return a specific type object given a Type parameter can be /// used as generic method and casting is not required. /// <example> /// instancecreationeditor.CreateInstance<int>(context); /// </example> /// </summary> public static T CreateInstance <T>(this InstanceCreationEditor instancecreationeditor, ITypeDescriptorContext context) { if (instancecreationeditor == null) { throw new ArgumentNullException("instancecreationeditor"); } return((T)instancecreationeditor.CreateInstance(context, typeof(T))); }