1 namespace System.Activities.Presentation.PropertyEditing {
4 using System.Reflection;
6 using System.Windows.Markup;
7 using System.Windows.Media.Imaging;
8 using System.Windows.Controls;
9 using System.Activities.Presentation.Internal;
10 using System.Diagnostics.CodeAnalysis;
11 using System.Activities.Presentation;
14 /// Base class that represents a factory for creating new items for a collection or
15 /// for a property value. 3rd party control developers may choose to derive from this class
16 /// to override the default behavior of specifying names and images that are used by
17 /// collection editor and sub-property editor when creating instances of custom controls.
19 class NewItemFactory {
21 private Type[] NoTypes = new Type[0];
24 /// Default constructor
26 public NewItemFactory() { }
29 /// Returns an object that can be set as the Content of a ContentControl
30 /// and that will be used an icon for the requested type by the property editing host.
31 /// The default implementation of this method uses naming convention, searching for
32 /// the embedded resources in the same assembly as the control, that are named the
33 /// same as the control (including namespace), followed by ".Icon", followed by
34 /// the extension for the file type itself. Currently, only ".png", ".bmp", ".gif",
35 /// ".jpg", and ".jpeg" extensions are recognized.
37 /// <param name="type">Type of the object to look up</param>
38 /// <param name="desiredSize">The desired size of the image to retrieve. If multiple
39 /// images are available this method retrieves the image that most closely
40 /// resembles the requested size. However, it is not guaranteed to return an image
41 /// that matches the desired size exactly.</param>
42 /// <returns>An image for the specified type.</returns>
43 /// <exception cref="ArgumentNullException">If type is null</exception>
44 public virtual object GetImage(Type type, Size desiredSize) {
46 throw FxTrace.Exception.ArgumentNull("type");
48 return ManifestImages.GetImage(type, desiredSize);
52 /// Returns the name for the item this factory adds for the passed in type. This is
53 /// the name that will be used in the "Add Item" drop down to identify the type being added.
54 /// The default implementation returns the short type name.
56 /// <param name="type">Type to retrieve the display name for.</param>
57 /// <returns>The display name for the specified type</returns>
58 /// <exception cref="ArgumentNullException">If type is null</exception>
59 [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "The intended usage in the larger scope of the class is the stronger type")]
60 public virtual string GetDisplayName(Type type)
63 throw FxTrace.Exception.ArgumentNull("type");
69 /// Returns an instance of an item that is added to the collection for the passed in Type.
70 /// The default implementation looks for public constructors that take no arguments.
71 /// If no such constructors are found, null is returned.
73 /// <param name="type">Type of the object to create</param>
74 /// <returns>Instance of the specified type, or null if no appropriate constructor was found
76 /// <exception cref="ArgumentNullException">If type is null</exception>
77 public virtual object CreateInstance(Type type) {
79 throw FxTrace.Exception.ArgumentNull("type");
81 ConstructorInfo ctor = type.GetConstructor(
82 BindingFlags.Public | BindingFlags.Instance,
87 return ctor == null ? null : ctor.Invoke(null);