< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Xml.Serialization.ConstantMappingSurrogate
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Xml/Serialization/XmlMembersMappingExtensions.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 4
Coverable lines: 4
Total lines: 412
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%110%
.ctor(...)100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Xml/Serialization/XmlMembersMappingExtensions.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Linq.Expressions;
 7using System.Reflection;
 8using System.Runtime.CompilerServices;
 9using System.Text;
 10using System.Xml;
 11using System.Xml.Schema;
 12using System.Xml.Serialization;
 13using CoreWCF.Runtime;
 14using CoreWCF.Runtime.Serialization;
 15
 16namespace CoreWCF.Xml.Serialization
 17{
 18    internal static class XmlMembersMappingExtensions
 19    {
 20        private static Func<XmlMapping, object> s_GetScopeDelegate = ReflectionHelper.GetPropertyDelegate<XmlMapping, ob
 21
 22        internal static object GetScope(this XmlMembersMapping xmlMembersMapping)
 23        {
 24            return s_GetScopeDelegate(xmlMembersMapping);
 25        }
 26
 27        private static Func<XmlMapping, object> s_GetAccessorDelegate = ReflectionHelper.GetPropertyDelegate<XmlMapping,
 28
 29        internal static ElementAccessorSurrogate GetAccessor(this XmlMembersMapping xmlMembersMapping)
 30        {
 31            var accessor = s_GetAccessorDelegate(xmlMembersMapping);
 32            return new ElementAccessorSurrogate(accessor);
 33        }
 34
 35        internal static ArrayMappingSurrogate AsArrayMappingSurrogate(this MappingSurrogate mappingSurrogate)
 36        {
 37            if (mappingSurrogate is ArrayMappingSurrogate) return mappingSurrogate as ArrayMappingSurrogate;
 38            return new ArrayMappingSurrogate(mappingSurrogate.WrappedObject);
 39        }
 40
 41        internal static EnumMappingSurrogate AsEnumMappingSurrogate(this MappingSurrogate mappingSurrogate)
 42        {
 43            if (mappingSurrogate is EnumMappingSurrogate) return mappingSurrogate as EnumMappingSurrogate;
 44            return new EnumMappingSurrogate(mappingSurrogate.WrappedObject);
 45        }
 46
 47        internal static PrimitiveMappingSurrogate AsPrimitiveMappingSurrogate(this MappingSurrogate mappingSurrogate)
 48        {
 49            if (mappingSurrogate is PrimitiveMappingSurrogate) return mappingSurrogate as PrimitiveMappingSurrogate;
 50            return new PrimitiveMappingSurrogate(mappingSurrogate.WrappedObject);
 51        }
 52
 53        internal static StructMappingSurrogate AsStructMappingSurrogate(this MappingSurrogate mappingSurrogate)
 54        {
 55            if (mappingSurrogate is StructMappingSurrogate) return mappingSurrogate as StructMappingSurrogate;
 56            return new StructMappingSurrogate(mappingSurrogate.WrappedObject);
 57        }
 58
 59        internal static NullableMappingSurrogate AsNullableMappingSurrogate(this MappingSurrogate mappingSurrogate)
 60        {
 61            if (mappingSurrogate is NullableMappingSurrogate) return mappingSurrogate as NullableMappingSurrogate;
 62            return new NullableMappingSurrogate(mappingSurrogate.WrappedObject);
 63        }
 64
 65        internal static MembersMappingSurrogate AsMembersMappingSurrogate(this MappingSurrogate mappingSurrogate)
 66        {
 67            if (mappingSurrogate is MembersMappingSurrogate) return mappingSurrogate as MembersMappingSurrogate;
 68            return new MembersMappingSurrogate(mappingSurrogate.WrappedObject);
 69        }
 70
 71        internal static XmlAttribute CreateXmlAttribute(string prefix, string localName, string namespaceURI, XmlDocumen
 72        {
 73            return new XmlAttributeImpl(prefix, localName, namespaceURI, doc);
 74        }
 75
 76        private class XmlAttributeImpl : XmlAttribute
 77        {
 78            public XmlAttributeImpl(string prefix, string localName, string namespaceURI, XmlDocument doc) : base(prefix
 79        }
 80    }
 81
 82    internal class AccessorSurrogate
 83    {
 84        internal static Type AccessorType { get; } = typeof(XmlElementAttribute).Assembly.GetType("System.Xml.Serializat
 85        private static Func<object, object> s_getMappingDelegate = ReflectionHelper.GetPropertyDelegate<object>(Accessor
 86        private static Func<object, string> s_getNameDelegate = ReflectionHelper.GetPropertyDelegate<string>(AccessorTyp
 87        private static Func<object, string> s_getNamespaceDelegate = ReflectionHelper.GetPropertyDelegate<string>(Access
 88
 89        internal object WrappedAccessor { get; }
 90
 91        public AccessorSurrogate(object accessor) => WrappedAccessor = accessor;
 92
 93        public MappingSurrogate Mapping => new MappingSurrogate(s_getMappingDelegate(WrappedAccessor));
 94        public string Name => s_getNameDelegate(WrappedAccessor);
 95        public string Namespace => s_getNamespaceDelegate(WrappedAccessor);
 96    }
 97
 98    internal class ElementAccessorSurrogate : AccessorSurrogate
 99    {
 100        internal static Type ElementAccessorType { get; } = typeof(XmlElementAttribute).Assembly.GetType("System.Xml.Ser
 101        private static Func<object, bool> s_getIsNullableDelegate = ReflectionHelper.GetPropertyDelegate<bool>(ElementAc
 102
 103        public ElementAccessorSurrogate(object accessor) : base(accessor) { }
 104
 105        public bool IsNullable => (bool)s_getIsNullableDelegate(WrappedAccessor);
 106    }
 107
 108    internal class MappingSurrogate
 109    {
 110        internal object WrappedObject { get; }
 111
 112        public MappingSurrogate(object mapping) => WrappedObject = mapping;
 113
 114        internal bool IsArrayMapping => WrappedObject.GetType() == ArrayMappingSurrogate.ArrayMappingType;
 115        public bool IsEnumMapping => WrappedObject.GetType() == EnumMappingSurrogate.EnumMappingType;
 116        public bool IsPrimitiveMapping => WrappedObject.GetType() == PrimitiveMappingSurrogate.PrimitiveMappingType;
 117        public bool IsStructMapping => WrappedObject.GetType() == StructMappingSurrogate.StructMappingType;
 118        public bool IsNullableMapping => WrappedObject.GetType() == NullableMappingSurrogate.NullableMappingType;
 119        public bool IsMembersMapping => WrappedObject.GetType() == MembersMappingSurrogate.MembersMappingType;
 120
 121        public override bool Equals(object obj)
 122        {
 123            if (obj is MappingSurrogate other)
 124            {
 125                return ReferenceEquals(WrappedObject, other.WrappedObject);
 126            }
 127
 128            return false;
 129        }
 130
 131        public override int GetHashCode()
 132        {
 133            return RuntimeHelpers.GetHashCode(WrappedObject);
 134        }
 135    }
 136
 137    internal class TypeMappingSurrogate : MappingSurrogate
 138    {
 139        internal static Type TypeMappingType { get; } = typeof(XmlElementAttribute).Assembly.GetType("System.Xml.Seriali
 140        private static Func<object, string> s_getTypeNameDelegate = ReflectionHelper.GetPropertyDelegate<string>(TypeMap
 141        private static Func<object, string> s_getNamespaceDelegate = ReflectionHelper.GetPropertyDelegate<string>(TypeMa
 142        private static Func<object, object> s_getTypeDescDelegate = ReflectionHelper.GetPropertyDelegate<object>(TypeMap
 143        private static Func<object, bool> s_getIncludeInSchemaDelegate = ReflectionHelper.GetPropertyDelegate<bool>(Type
 144
 145        public TypeMappingSurrogate(object mapping) : base(mapping) { }
 146        internal string TypeName => s_getTypeNameDelegate(WrappedObject);
 147        internal string Namespace => s_getNamespaceDelegate(WrappedObject);
 148        internal TypeDescSurrogate TypeDesc => new TypeDescSurrogate(s_getTypeDescDelegate(WrappedObject));
 149        internal bool IncludeInSchema => s_getIncludeInSchemaDelegate(WrappedObject);
 150    }
 151
 152    internal class ArrayMappingSurrogate : TypeMappingSurrogate
 153    {
 154        internal static Type ArrayMappingType { get; } = typeof(XmlElementAttribute).Assembly.GetType("System.Xml.Serial
 155        private static Func<object, object> s_getNextDelegate = ReflectionHelper.GetPropertyDelegate<object>(ArrayMappin
 156        private static Func<object, object[]> s_getElementsDelegate = ReflectionHelper.GetPropertyDelegate<object[]>(Arr
 157
 158        private ElementAccessorSurrogate[] _elementAccessorSurrogateArray;
 159
 160        internal ArrayMappingSurrogate(object arrayMapping) : base(arrayMapping) { }
 161
 162        public ArrayMappingSurrogate Next
 163        {
 164            get
 165            {
 166                var nextObj = s_getNextDelegate(WrappedObject);
 167                return nextObj == null ? null : new ArrayMappingSurrogate(nextObj);
 168            }
 169        }
 170
 171        public ElementAccessorSurrogate[] Elements => GetElementAccessorSurrogateArray();
 172
 173        private ElementAccessorSurrogate[] GetElementAccessorSurrogateArray()
 174        {
 175            if (_elementAccessorSurrogateArray == null)
 176            {
 177                var elementsArray = s_getElementsDelegate(WrappedObject);
 178                _elementAccessorSurrogateArray = new ElementAccessorSurrogate[elementsArray.Length];
 179                for (int i = 0; i < elementsArray.Length; i++)
 180                {
 181                    _elementAccessorSurrogateArray[i] = new ElementAccessorSurrogate(elementsArray[i]);
 182                }
 183            }
 184
 185            return _elementAccessorSurrogateArray;
 186        }
 187    }
 188
 189    internal class PrimitiveMappingSurrogate : TypeMappingSurrogate
 190    {
 191        internal static Type PrimitiveMappingType { get; } = typeof(XmlElementAttribute).Assembly.GetType("System.Xml.Se
 192
 193        public PrimitiveMappingSurrogate(object primitiveMapping) : base(primitiveMapping)
 194        {
 195        }
 196    }
 197
 198    internal class NullableMappingSurrogate : TypeMappingSurrogate
 199    {
 200        internal static Type NullableMappingType { get; } = typeof(XmlElementAttribute).Assembly.GetType("System.Xml.Ser
 201        private static Func<object, object> s_getBaseMappingDelegate = ReflectionHelper.GetPropertyDelegate<object>(Null
 202
 203        public NullableMappingSurrogate(object mapping) : base(mapping) { }
 204
 205        internal MappingSurrogate BaseMapping
 206        {
 207            get
 208            {
 209                var baseMappingObj = s_getBaseMappingDelegate(WrappedObject);
 210                return baseMappingObj == null ? null : new MappingSurrogate(baseMappingObj);
 211            }
 212        }
 213    }
 214
 215    internal class StructMappingSurrogate : TypeMappingSurrogate
 216    {
 217        internal static Type StructMappingType { get; } = typeof(XmlElementAttribute).Assembly.GetType("System.Xml.Seria
 218        private static Func<object, object> s_getDerivedMappingsDelegate = ReflectionHelper.GetPropertyDelegate<object>(
 219        private static Func<object, object> s_getNextDerivedMappingDelegate = ReflectionHelper.GetPropertyDelegate<objec
 220        private static Func<object, object> s_getBaseMappingDelegate = ReflectionHelper.GetPropertyDelegate<object>(Stru
 221        private static Func<object, object[]> s_getMembersDelegate = ReflectionHelper.GetPropertyDelegate<object[]>(Stru
 222
 223        private MemberMappingSurrogate[] _memberMappingSurrogateArray;
 224
 225        public StructMappingSurrogate(object mapping) : base(mapping)
 226        {
 227        }
 228
 229        internal MemberMappingSurrogate[] Members => GetMemberMappingSurrogateArray();
 230
 231        private MemberMappingSurrogate[] GetMemberMappingSurrogateArray()
 232        {
 233            if (_memberMappingSurrogateArray == null)
 234            {
 235                var membersArray = s_getMembersDelegate(WrappedObject);
 236                _memberMappingSurrogateArray = new MemberMappingSurrogate[membersArray.Length];
 237                for (int i = 0; i < membersArray.Length; i++)
 238                {
 239                    _memberMappingSurrogateArray[i] = new MemberMappingSurrogate(membersArray[i]);
 240                }
 241            }
 242
 243            return _memberMappingSurrogateArray;
 244        }
 245
 246        public StructMappingSurrogate DerivedMappings
 247        {
 248            get
 249            {
 250                var derivedMappingObj = s_getDerivedMappingsDelegate(WrappedObject);
 251                return derivedMappingObj == null ? null : new StructMappingSurrogate(derivedMappingObj);
 252            }
 253        }
 254
 255        public StructMappingSurrogate NextDerivedMapping
 256        {
 257            get
 258            {
 259                var nextDerivedMappingObj = s_getNextDerivedMappingDelegate(WrappedObject);
 260                return nextDerivedMappingObj == null ? null : new StructMappingSurrogate(nextDerivedMappingObj);
 261            }
 262        }
 263
 264        public StructMappingSurrogate BaseMapping
 265        {
 266            get
 267            {
 268                var derivedMappingObj = s_getBaseMappingDelegate(WrappedObject);
 269                return derivedMappingObj == null ? null : new StructMappingSurrogate(derivedMappingObj);
 270            }
 271        }
 272    }
 273
 274    internal class EnumMappingSurrogate : PrimitiveMappingSurrogate
 275    {
 276        internal static Type EnumMappingType { get; } = typeof(XmlElementAttribute).Assembly.GetType("System.Xml.Seriali
 277        private static Func<object, bool> s_getIsFlagsDelegate = ReflectionHelper.GetPropertyDelegate<bool>(EnumMappingT
 278        private static Func<object, object[]> s_getConstantsDelegate = ReflectionHelper.GetPropertyDelegate<object[]>(En
 279
 280        private ConstantMappingSurrogate[] _constantsMappingSurrogateArray;
 281
 282        public EnumMappingSurrogate(object enumMapping) : base(enumMapping) { }
 283
 284        public ConstantMappingSurrogate[] Constants => GetConstantsMappingSurrogateArray();
 285
 286        public bool IsFlags => s_getIsFlagsDelegate(WrappedObject);
 287
 288        private ConstantMappingSurrogate[] GetConstantsMappingSurrogateArray()
 289        {
 290            if (_constantsMappingSurrogateArray == null)
 291            {
 292                var constantsArray = s_getConstantsDelegate(WrappedObject);
 293                _constantsMappingSurrogateArray = new ConstantMappingSurrogate[constantsArray.Length];
 294                for (int i = 0; i < constantsArray.Length; i++)
 295                {
 296                    _constantsMappingSurrogateArray[i] = new ConstantMappingSurrogate(constantsArray[i]);
 297                }
 298            }
 299
 300            return _constantsMappingSurrogateArray;
 301        }
 302    }
 303
 304    internal class ConstantMappingSurrogate : MappingSurrogate
 305    {
 0306        internal static Type ConstantMappingType { get; } = typeof(XmlElementAttribute).Assembly.GetType("System.Xml.Ser
 0307        private static Func<object, string> s_getTypeNameDelegate = ReflectionHelper.GetPropertyDelegate<string>(Constan
 308
 0309        public ConstantMappingSurrogate(object mapping) : base(mapping) { }
 310
 0311        public string XmlName => s_getTypeNameDelegate(WrappedObject);
 312    }
 313
 314    internal class AccessorMappingSurrogate : MappingSurrogate
 315    {
 316        internal static Type AccessorMappingType { get; } = typeof(XmlElementAttribute).Assembly.GetType("System.Xml.Ser
 317        private static Func<object, object[]> s_getElementsDelegate = ReflectionHelper.GetPropertyDelegate<object[]>(Acc
 318        private static Func<object, object> s_getTypeDescDelegate = ReflectionHelper.GetPropertyDelegate<object>(Accesso
 319
 320        private ElementAccessorSurrogate[] _elementAccessorSurrogateArray;
 321
 322        public AccessorMappingSurrogate(object mapping) : base(mapping) { }
 323
 324        internal TypeDescSurrogate TypeDesc => new TypeDescSurrogate(s_getTypeDescDelegate(WrappedObject));
 325
 326        internal ElementAccessorSurrogate[] Elements => GetElementAccessorSurrogateArray();
 327
 328        private ElementAccessorSurrogate[] GetElementAccessorSurrogateArray()
 329        {
 330            if (_elementAccessorSurrogateArray == null)
 331            {
 332                var elementsArray = s_getElementsDelegate(WrappedObject);
 333                _elementAccessorSurrogateArray = new ElementAccessorSurrogate[elementsArray.Length];
 334                for (int i = 0; i < elementsArray.Length; i++)
 335                {
 336                    _elementAccessorSurrogateArray[i] = new ElementAccessorSurrogate(elementsArray[i]);
 337                }
 338            }
 339
 340            return _elementAccessorSurrogateArray;
 341        }
 342    }
 343
 344    internal class MemberMappingSurrogate : AccessorMappingSurrogate
 345    {
 346        internal static Type MemberMappingType { get; } = typeof(XmlElementAttribute).Assembly.GetType("System.Xml.Seria
 347        private static Func<object, int> s_getCheckSpecifiedDelegate = ReflectionHelper.GetPropertyDelegate<int>(MemberM
 348        private static Func<object, bool> s_getCheckShouldPersistDelegate = ReflectionHelper.GetPropertyDelegate<bool>(M
 349
 350        public MemberMappingSurrogate(object memberMapping) : base(memberMapping) { }
 351
 352        public SpecifiedAccessor CheckSpecified => (SpecifiedAccessor)s_getCheckSpecifiedDelegate(WrappedObject);
 353
 354        public bool CheckShouldPersist => s_getCheckShouldPersistDelegate(WrappedObject);
 355
 356        internal enum SpecifiedAccessor
 357        {
 358            None,
 359            ReadOnly,
 360            ReadWrite,
 361        }
 362    }
 363
 364    internal class MembersMappingSurrogate : TypeMappingSurrogate
 365    {
 366        internal static Type MembersMappingType { get; } = typeof(XmlElementAttribute).Assembly.GetType("System.Xml.Seri
 367        private static Func<object, object[]> s_getMembersDelegate = ReflectionHelper.GetPropertyDelegate<object[]>(Memb
 368
 369        public MembersMappingSurrogate(object mapping) : base(mapping) { }
 370
 371        private MemberMappingSurrogate[] _memberMappingSurrogateArray;
 372        internal virtual MemberMappingSurrogate[] Members => GetMemberMappingSurrogateArray();
 373        private MemberMappingSurrogate[] GetMemberMappingSurrogateArray()
 374        {
 375            if (_memberMappingSurrogateArray == null)
 376            {
 377                var membersArray = s_getMembersDelegate(WrappedObject);
 378                _memberMappingSurrogateArray = new MemberMappingSurrogate[membersArray.Length];
 379                for (int i = 0; i < membersArray.Length; i++)
 380                {
 381                    _memberMappingSurrogateArray[i] = new MemberMappingSurrogate(membersArray[i]);
 382                }
 383            }
 384
 385            return _memberMappingSurrogateArray;
 386        }
 387    }
 388
 389    internal class TypeDescSurrogate
 390    {
 391        internal static Type TypeDescType { get; } = typeof(XmlElementAttribute).Assembly.GetType("System.Xml.Serializat
 392        private static Func<object, bool> s_getIsXsdTypeDelegate = ReflectionHelper.GetPropertyDelegate<bool>(TypeDescTy
 393        private static Func<object, bool> s_getIsRootDelegate = ReflectionHelper.GetPropertyDelegate<bool>(TypeDescType,
 394        private static Func<object, bool> s_getIsAbstractDelegate = ReflectionHelper.GetPropertyDelegate<bool>(TypeDescT
 395        private static Func<object, XmlSchemaType> s_getDataTypeDelegate = ReflectionHelper.GetPropertyDelegate<XmlSchem
 396        private static Func<object, string> s_getNameDelegate = ReflectionHelper.GetPropertyDelegate<string>(TypeDescTyp
 397        private static Func<object, bool> s_getIsValueTypeDelegate = ReflectionHelper.GetPropertyDelegate<bool>(TypeDesc
 398
 399        private object _typeDesc;
 400
 401        public TypeDescSurrogate(object typeDesc) => _typeDesc = typeDesc;
 402
 403        public bool IsXsdType => s_getIsXsdTypeDelegate(_typeDesc);
 404        public bool IsRoot => s_getIsRootDelegate(_typeDesc);
 405        public bool IsAbstract => s_getIsAbstractDelegate(_typeDesc);
 406        public XmlSchemaType DataType => s_getDataTypeDelegate(_typeDesc);
 407        public string Name => s_getNameDelegate(_typeDesc);
 408        public bool IsValueType => s_getIsValueTypeDelegate(_typeDesc);
 409    }
 410
 411
 412}