[reflection] Coop handles icalls in System.Reflection and System.RuntimeTypeHandle...
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Xaml / ActivityBuilderHelper.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.Xaml
6 {
7     using System.Activities.Presentation.Model;
8     using System.Collections.Generic;
9
10     internal static class ActivityBuilderHelper
11     {
12         internal static bool IsActivityBuilderType(ModelItem modelItem)
13         {
14             if (null == modelItem)
15             {
16                 throw FxTrace.Exception.AsError(new ArgumentNullException("modelItem"));
17             }
18             return modelItem.ItemType.IsAssignableFrom(typeof(ActivityBuilder));
19         }
20
21         internal static List<Variable> GetVariables(object input)
22         {
23             if (null == input)
24             {
25                 throw FxTrace.Exception.AsError(new ArgumentNullException("input"));
26             }
27             ModelItem astAsModelItem = input as ModelItem;
28             ActivityBuilder instance = input as ActivityBuilder;
29             if (null != astAsModelItem)
30             {
31                 if (!astAsModelItem.ItemType.IsAssignableFrom(typeof(ActivityBuilder)))
32                 {
33                     throw FxTrace.Exception.AsError(new InvalidCastException(astAsModelItem.ItemType.FullName));
34                 }
35                 instance = (ActivityBuilder)astAsModelItem.GetCurrentValue();
36             }
37             else if (null == instance)
38             {
39                 throw FxTrace.Exception.AsError(new InvalidCastException(input.GetType().FullName));
40             }
41
42             List<Variable> variables = new List<Variable>();
43             foreach (DynamicActivityProperty property in instance.Properties)
44             {
45                 if (property != null)
46                 {
47                     Variable autoVariable = GetVariableFromProperty(property);
48                     if (autoVariable != null)
49                     {
50                         variables.Add(autoVariable);
51                     }
52                 }
53             }
54             return variables;
55         }
56
57         internal static Variable GetVariableFromProperty(DynamicActivityProperty property)
58         {
59             Type variableType = null;
60             Variable autoVariable = null;
61
62             if (property.Type != null)
63             {
64                 Type propertyType = property.Type;
65
66                 // if the property is an Argument<T> create a variable of type T
67                 if (propertyType != null && typeof(Argument).IsAssignableFrom(propertyType))
68                 {
69                     if (propertyType.IsGenericType)
70                     {
71                         variableType = propertyType.GetGenericArguments()[0];
72                     }
73                     else
74                     {
75                         variableType = typeof(object);
76                     }
77                 }
78             }
79             if (variableType != null)
80             {
81                 autoVariable = Variable.Create(property.Name, variableType, VariableModifiers.None);
82                 Argument argument = property.Value as Argument;
83                 if (argument != null)
84                 {
85                     autoVariable.Default = argument.Expression;
86                 }
87             }
88             return autoVariable;
89         }
90     }
91 }