[corlib] Avoid unnecessary ephemeron array resizes
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / View / PropertyEntryToEditorOptionConverter.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.View
6 {
7     using System;
8     using System.Collections.Generic;
9     using System.Diagnostics.CodeAnalysis;
10     using System.Globalization;
11     using System.Reflection;
12     using System.Windows.Data;
13     using System.Activities.Presentation.Model;
14     using System.Activities.Presentation.PropertyEditing;
15     using System.Activities.Presentation.Internal.PropertyEditing.Model;
16     using System.Runtime;
17
18     sealed class PropertyEntryToEditorOptionConverter : IValueConverter
19     {
20         public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
21         {
22             PropertyEntry propertyEntry = value as PropertyEntry;
23             if (null == propertyEntry)
24             {
25                 PropertyValue propertyValue = value as PropertyValue;
26                 if (null != propertyValue)
27                 {
28                     propertyEntry = propertyValue.ParentProperty;
29                 }
30             }
31
32             ModelPropertyEntry modelPropertyEntry = propertyEntry as ModelPropertyEntry;
33
34             ModelProperty modelProperty = null;
35             if (modelPropertyEntry != null)
36             {
37                 modelProperty = modelPropertyEntry.FirstModelProperty;
38             }
39
40             if (modelProperty == null)
41             {
42                 return Binding.DoNothing;
43             }
44
45             string optionName = parameter as string;
46             if (optionName == null)
47             {
48                 return Binding.DoNothing;
49             }
50
51             object optionValue;
52             if (EditorOptionAttribute.TryGetOptionValue(modelProperty.Attributes, optionName, out optionValue))
53             {
54                 return optionValue;
55             }
56
57             return Binding.DoNothing;
58         }
59
60         public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
61         {
62             // It is simply impossible to convert back.
63             throw FxTrace.Exception.AsError(new NotSupportedException());
64         }
65     }
66 }