File: ExtensionElementCollection.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,284,488 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (114 lines) | stat: -rw-r--r-- 4,562 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------------------------

namespace System.ServiceModel.Configuration
{
    using System;
    using System.Configuration;

    [ConfigurationCollection(typeof(ExtensionElement), CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class ExtensionElementCollection : ServiceModelConfigurationElementCollection<ExtensionElement>
    {
        public ExtensionElementCollection()
            : base(ConfigurationElementCollectionType.BasicMap, ConfigurationStrings.Add)
        {
        }

        protected override void BaseAdd(ConfigurationElement element)
        {
            if (!this.InheritedElementExists((ExtensionElement)element))
            {
                this.EnforceUniqueElement((ExtensionElement)element);
                base.BaseAdd(element);
            }
        }

        protected override void BaseAdd(int index, ConfigurationElement element)
        {
            if (!this.InheritedElementExists((ExtensionElement)element))
            {
                this.EnforceUniqueElement((ExtensionElement)element);
                base.BaseAdd(index, element);
            }
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            if (null == element)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("element");
            }

            ExtensionElement configElementKey = (ExtensionElement)element;
            return configElementKey.Name;
        }

        bool InheritedElementExists(ExtensionElement element)
        {
            // This is logic from ServiceModelEnhancedConfigurationElementCollection
            // The idea is to allow duplicate identitcal extension definition in different level (i.e. app level and machine level)
            // We however do not allow them on the same level.
            // Identical extension is defined by same name and type.
            object newElementKey = this.GetElementKey(element);
            if (this.ContainsKey(newElementKey))
            {
                ExtensionElement oldElement = (ExtensionElement)this.BaseGet(newElementKey);
                if (null != oldElement)
                {
                    // Is oldElement present in the different level of original config
                    // and name/type matching
                    if (!oldElement.ElementInformation.IsPresent &&
                        element.Type.Equals(oldElement.Type, StringComparison.Ordinal))
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        void EnforceUniqueElement(ExtensionElement element)
        {
            foreach (ExtensionElement extension in this)
            {
                if (element.Name.Equals(extension.Name, StringComparison.Ordinal))
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
                        SR.GetString(SR.ConfigDuplicateExtensionName, element.Name)));
                }

                bool foundDuplicateType = false;
                if (element.Type.Equals(extension.Type, StringComparison.OrdinalIgnoreCase))
                {
                    foundDuplicateType = true;
                }
                else if (element.TypeName.Equals(extension.TypeName, StringComparison.Ordinal))
                {
                    // In order to avoid extra assemblies being loaded, we perform type comparison only if the type names
                    // are the same. See bug CSDMain 222573.
                    Type elementType = Type.GetType(element.Type, false);
                    if (null != elementType && elementType.Equals(Type.GetType(extension.Type, false)))
                    {
                        foundDuplicateType = true;
                    }
                }

                if (foundDuplicateType)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
                        SR.GetString(SR.ConfigDuplicateExtensionType, element.Type)));
                }
            }
        }

        protected override bool ThrowOnDuplicate
        {
            get
            {
                return true;
            }
        }
    }
}