[reflection] Coop handles icalls in System.Reflection and System.RuntimeTypeHandle...
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / PropertyReferenceUtilities.cs
1 //----------------------------------------------------------------
2 // <copyright company="Microsoft Corporation">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //----------------------------------------------------------------
6
7 namespace System.Activities.Presentation
8 {
9     using System.Collections.Generic;
10     using System.Runtime;
11
12     internal static class PropertyReferenceUtilities
13     {
14         public static string GetPropertyReference(object instance, string targetProperty)
15         {
16             Fx.Assert(instance != null, "instance should not be null.");
17             Fx.Assert(!string.IsNullOrEmpty(targetProperty), "targetProperty should not be null or empty.");
18
19             IList<ActivityPropertyReference> references = ActivityBuilder.GetPropertyReferences(instance);
20
21             Fx.Assert(references != null, "references should not be null");
22
23             foreach (ActivityPropertyReference reference in references)
24             {
25                 if (StringComparer.Ordinal.Equals(reference.TargetProperty, targetProperty))
26                 {
27                     return reference.SourceProperty;
28                 }
29             }
30             
31             return null;
32         }
33
34         public static void SetPropertyReference(object instance, string targetProperty, string sourceProperty)
35         {
36             Fx.Assert(instance != null, "instance should not be null.");
37             Fx.Assert(!string.IsNullOrEmpty(targetProperty), "targetProperty should not be null or empty.");
38
39             ActivityPropertyReference entry = null;
40             IList<ActivityPropertyReference> references = ActivityBuilder.GetPropertyReferences(instance);
41
42             Fx.Assert(references != null, "references should not be null");
43
44             foreach (ActivityPropertyReference reference in references)
45             {
46                 if (StringComparer.Ordinal.Equals(reference.TargetProperty, targetProperty))
47                 {
48                     entry = reference;
49                     break;
50                 }
51             }
52
53             if (string.IsNullOrEmpty(sourceProperty))
54             {
55                 if (entry != null)
56                 {
57                     references.Remove(entry);
58                 }
59             }
60             else
61             {
62                 if (entry != null)
63                 {
64                     entry.SourceProperty = sourceProperty;
65                 }
66                 else
67                 {
68                     entry = new ActivityPropertyReference();
69                     entry.TargetProperty = targetProperty;
70                     entry.SourceProperty = sourceProperty;
71                     references.Add(entry);
72                 }
73             }
74         }
75     }
76 }