5b106a83a21b768ae60560bbff8033cc4d926065
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Internal / PropertyEditing / FromExpression / Framework / PropertyInspector / ExtensibilityMetadataHelper.cs
1
2 // -------------------------------------------------------------------
3 // Copyright (c) Microsoft Corporation. All Rights Reserved.
4 // -------------------------------------------------------------------
5 //From \\authoring\Sparkle\Source\1.0.1083.0\Common\Source\Framework\Properties
6 namespace System.Activities.Presentation.Internal.PropertyEditing.FromExpression.Framework.PropertyInspector
7 {
8     using System.ComponentModel;
9     using System.Diagnostics;
10     using System.Collections.Generic;
11     using System.Collections.ObjectModel;
12     using System;
13     using System.Activities.Presentation.PropertyEditing;
14     using System.Collections;
15     using System.Globalization;
16     using System.Runtime;
17     using System.Diagnostics.CodeAnalysis;
18
19     internal static class ExtensibilityMetadataHelper
20     {
21         // <summary>
22         // Returns an instance of the PropertyValueEditor specified in the provided attribute list.
23         // </summary>
24         // <param name="attributes">A list of attributes. If an EditorAttribute is not specified in this collection, will return null.</param>
25         // <param name="exceptionLogger">Interface for exception logging. If null, exceptions will be silently ignored.</param>
26         // <returns></returns>
27         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Propagating the error might cause VS to crash")]
28         [SuppressMessage("Reliability", "Reliability108", Justification = "Propagating the error might cause VS to crash")]
29         public static PropertyValueEditor GetValueEditor(IEnumerable attributes, IMessageLogger exceptionLogger)
30         {
31             PropertyValueEditor propertyValueEditor = null;
32             if (attributes != null)
33             {
34                 foreach (Attribute attribute in attributes)
35                 {
36                     EditorAttribute editorAttribute = attribute as EditorAttribute;
37                     if (editorAttribute != null)
38                     {
39                         try
40                         {
41                             Type editorType = Type.GetType(editorAttribute.EditorTypeName);
42                             if (editorType != null && typeof(PropertyValueEditor).IsAssignableFrom(editorType))
43                             {
44                                 propertyValueEditor = (PropertyValueEditor)Activator.CreateInstance(editorType);
45                                 break;
46                             }
47                         }
48                         catch (Exception e)
49                         {
50                             if (exceptionLogger != null)
51                             {
52                                 exceptionLogger.WriteLine(string.Format(CultureInfo.CurrentCulture, ExceptionStringTable.ValueEditorLoadFailed, ExtensibilityMetadataHelper.GetExceptionMessage(e)));
53                             }
54                         }
55                     }
56                 }
57             }
58             return propertyValueEditor;
59         }
60
61         // <summary>
62         // Returns the type of the editor specified by the provided EditorAttribute.
63         // </summary>
64         // <param name="attribute">EditorAttribute that specifies a CategoryEditor type. If the type specified is not derived from CategoryEditor, or cannot be loaded, will return null.</param>
65         // <param name="exceptionLogger">Interface for exception logging. If null, exceptions will be silently ignored.</param>
66         // <returns></returns>
67         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Propagating the error might cause VS to crash")]
68         [SuppressMessage("Reliability", "Reliability108", Justification = "Propagating the error might cause VS to crash")]
69         public static Type GetCategoryEditorType(EditorAttribute attribute, IMessageLogger exceptionLogger)
70         {
71             try
72             {
73                 Type editorType = Type.GetType(attribute.EditorTypeName);
74                 if (editorType != null && typeof(CategoryEditor).IsAssignableFrom(editorType))
75                 {
76                     return editorType;
77                 }
78             }
79             catch (Exception e)
80             {
81                 if (exceptionLogger != null)
82                 {
83                     exceptionLogger.WriteLine(string.Format(CultureInfo.CurrentCulture, ExceptionStringTable.CategoryEditorTypeLoadFailed, ExtensibilityMetadataHelper.GetExceptionMessage(e)));
84                 }
85             }
86             return null;
87         }
88
89         public static string GetExceptionMessage(Exception e)
90         {
91             return (e.InnerException != null) ? e.InnerException.ToString() : e.Message;
92         }
93
94         public static bool IsEditorReusable(IEnumerable attributes)
95         {
96             bool isEditorReusable = true;
97             if (attributes != null)
98             {
99                 foreach (Attribute attribute in attributes)
100                 {
101                     EditorReuseAttribute editorReuseAttribute = attribute as EditorReuseAttribute;
102                     if (editorReuseAttribute != null)
103                     {
104                         isEditorReusable = editorReuseAttribute.ReuseEditor;
105                         break;
106                     }
107                 }
108             }
109             return isEditorReusable;
110         }
111     }
112 }
113