< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Description.CustomAttributeProvider
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Description/CustomAttributeProvider.cs
Line coverage
93%
Covered lines: 152
Uncovered lines: 11
Coverable lines: 163
Total lines: 374
Line coverage: 93.2%
Branch coverage
89%
Covered branches: 86
Total branches: 96
Branch coverage: 89.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Description/CustomAttributeProvider.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.Diagnostics.Contracts;
 6using System.Linq;
 7using System.Reflection;
 8using CoreWCF.Runtime;
 9
 10namespace CoreWCF.Description
 11{
 12    internal static class CustomAttributeProvider
 13    {
 14
 15        public static object[] GetDualCustomAttributes(this ICustomAttributeProvider provider, Type attributeType, bool 
 16        {
 5614917            object[] attributes = provider.GetCustomAttributes(inherit);
 5614718            if (attributes == null || attributes.Length == 0)
 19            {
 1737720                return attributes;
 21            }
 22
 9394523            object[] result = attributes.Where(attribute => attributeType.IsAssignableFrom(attribute.GetType())).ToArray
 24
 3877025            if (result.Length == 0)
 26            {
 27                // Only if we don't find the CoreWCF attribute, look for the S.SM attribute
 3336028                if (attributeType == typeof(ServiceContractAttribute))
 29                {
 397430                    result = attributes.Where(attribute => attribute.GetType().FullName.Equals(ServiceReflector.SMServic
 207431                    for (int i = 0; i < result.Length; i++)
 32                    {
 22633                        result[i] = ConvertFromServiceModelServiceContractAttribute(result[i]);
 34                    }
 35                }
 3254936                else if (attributeType == typeof(OperationContractAttribute))
 37                {
 725838                    result = attributes.Where(attribute => attribute.GetType().FullName.Equals(ServiceReflector.SMOperat
 641239                    for (int i = 0; i < result.Length; i++)
 40                    {
 19141                        result[i] = ConvertFromServiceModelOperationContractAttribute(result[i]);
 42                    }
 43                }
 2953444                else if (attributeType == typeof(MessageContractAttribute))
 45                {
 2246                    result = attributes.Where(attribute => attribute.GetType().FullName.Equals(ServiceReflector.SMMessag
 4447                    for (int i = 0; i < result.Length; i++)
 48                    {
 1149                        result[i] = ConvertFromServiceModelMessageContractAttribute(result[i]);
 50                    }
 51                }
 2952352                else if (attributeType == typeof(MessageHeaderAttribute))
 53                {
 23054                    result = attributes.Where(attribute => attribute.GetType().FullName.Equals(ServiceReflector.SMMessag
 23055                                                           attribute.GetType().FullName.Equals(ServiceReflector.SMMessag
 24256                    for (int i = 0; i < result.Length; i++)
 57                    {
 658                        result[i] = ConvertFromServiceModelMessageHeaderAttribute(result[i]);
 59                    }
 60                }
 2940861                else if (attributeType == typeof(MessageBodyMemberAttribute))
 62                {
 19863                    result = attributes.Where(attribute => attribute.GetType().FullName.Equals(ServiceReflector.SMMessag
 21464                    for (int i = 0; i < result.Length; i++)
 65                    {
 866                        result[i] = ConvertFromServiceModelMessageBodyMemberAttribute(result[i]);
 67                    }
 68                }
 2930969                else if (attributeType == typeof(MessagePropertyAttribute))
 70                {
 1071                    result = attributes.Where(attribute => attribute.GetType().FullName.Equals(ServiceReflector.SMMessag
 1672                    for (int i = 0; i < result.Length; i++)
 73                    {
 374                        result[i] = ConvertFromServiceModelMessagePropertyAttribute(result[i]);
 75                    }
 76                }
 2930477                else if (attributeType == typeof(MessageParameterAttribute))
 78                {
 34279                    result = attributes.Where(attribute => attribute.GetType().FullName.Equals(ServiceReflector.SMMessag
 39880                    for (int i = 0; i < result.Length; i++)
 81                    {
 2882                        result[i] = ConvertFromServiceModelMessageParameterAttribute(result[i]);
 83                    }
 84                }
 2913385                else if (attributeType == typeof(XmlSerializerFormatAttribute))
 86                {
 1271987                    result = attributes.Where(attribute => attribute.GetType().FullName.Equals(ServiceReflector.SMXmlSer
 1168288                    for (int i = 0; i < result.Length; i++)
 89                    {
 8090                        result[i] = ConvertFromServiceModelXmlSerializerFormatAttribute(result[i]);
 91                    }
 92                }
 2337293                else if (attributeType == typeof(FaultContractAttribute))
 94                {
 560695                   result = attributes.Where(attribute => attribute.GetType().FullName.Equals(ServiceReflector.SMFaultCo
 517696                   for (int i = 0; i < result.Length; i++)
 97                   {
 498                       result[i] = ConvertFromServiceModelFaultContractAttribute(result[i]);
 99                   }
 100                }
 20788101                else if (attributeType == typeof(ServiceKnownTypeAttribute))
 102                {
 7417103                    result = attributes.Where(attribute => attribute.GetType().FullName.Equals(ServiceReflector.SMServic
 6722104                    for (int i = 0; i < result.Length; i++)
 105                    {
 18106                        result[i] = ConvertFromServiceModelServiceKnownTypeAttribute(result[i]);
 107                    }
 108                }
 109            }
 38770110            return result;
 111        }
 112
 113        private static ServiceContractAttribute ConvertFromServiceModelServiceContractAttribute(object attr)
 114        {
 115            Fx.Assert(attr.GetType().FullName.Equals(ServiceReflector.SMServiceContractAttributeFullName), "Expected att
 226116            bool hasProtectionLevel = GetProperty<bool>(attr, "HasProtectionLevel");
 226117            if (hasProtectionLevel)
 118            {
 119                // ProtectionLevel isn't supported yet so if it was set on the S.SM.SCA, then we can't do the mapping so
 0120                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new PlatformNotSupportedException("System.Serv
 121            }
 122
 226123            var sca = new ServiceContractAttribute();
 226124            string tmpStr = GetProperty<string>(attr, nameof(ServiceContractAttribute.ConfigurationName));
 226125            if (!string.IsNullOrEmpty(tmpStr))
 126            {
 0127                sca.ConfigurationName = tmpStr;
 128            }
 129
 226130            tmpStr = GetProperty<string>(attr, nameof(ServiceContractAttribute.Name));
 226131            if (!string.IsNullOrEmpty(tmpStr))
 132            {
 36133                sca.Name = tmpStr;
 134            }
 135
 226136            sca.Namespace = GetProperty<string>(attr, nameof(ServiceContractAttribute.Namespace));
 226137            sca.SessionMode = GetProperty<SessionMode>(attr, nameof(ServiceContractAttribute.SessionMode));
 226138            sca.CallbackContract = GetProperty<Type>(attr, nameof(ServiceContractAttribute.CallbackContract));
 226139            return sca;
 140        }
 141
 142
 143        private static MessageContractAttribute ConvertFromServiceModelMessageContractAttribute(object attr)
 144        {
 145            Fx.Assert(attr.GetType().FullName.Equals(ServiceReflector.SMMessageContractAttributeFullName), "Expected att
 11146            var messageContract = new MessageContractAttribute
 11147            {
 11148                IsWrapped = GetProperty<bool>(attr, nameof(MessageContractAttribute.IsWrapped))
 11149            };
 11150            string tmpStr = GetProperty<string>(attr, nameof(MessageContractAttribute.WrapperName));
 11151            if (!string.IsNullOrEmpty(tmpStr))
 152            {
 11153                messageContract.WrapperName = tmpStr;
 154            }
 11155            tmpStr = GetProperty<string>(attr, nameof(MessageContractAttribute.WrapperNamespace));
 11156            if (!string.IsNullOrEmpty(tmpStr))
 157            {
 11158                messageContract.WrapperNamespace = tmpStr;
 159            }
 11160            return messageContract;
 161        }
 162
 163        private static MessageHeaderAttribute ConvertFromServiceModelMessageHeaderAttribute(object attr)
 164        {
 6165            string attributeFullName = attr.GetType().FullName;
 166            Fx.Assert(attributeFullName.Equals(ServiceReflector.SMMessageHeaderAttributeFullName) ||
 167                      attributeFullName.Equals(ServiceReflector.SMMessageHeaderArrayAttributeFullName),
 168                      "Expected attribute of type System.ServiceModel.MessageHeaderAttribute or System.ServiceModel.Mess
 169
 6170            bool hasProtectionLevel = GetProperty<bool>(attr, "HasProtectionLevel");
 6171            if (hasProtectionLevel)
 172            {
 173                // ProtectionLevel isn't supported yet so if it was set on the S.SM.SCA, then we can't do the mapping so
 0174                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new PlatformNotSupportedException("System.Serv
 175            }
 176
 177            MessageHeaderAttribute messageHeader;
 178            Type declaringPropertyType; // Used so that we call GetProperty on the SM MessageHeaderAttribute type and no
 6179            if (attributeFullName.Equals(ServiceReflector.SMMessageHeaderAttributeFullName))
 180            {
 6181                messageHeader = new MessageHeaderAttribute();
 6182                declaringPropertyType = attr.GetType();
 183            }
 184            else
 185            {
 0186                messageHeader = new MessageHeaderArrayAttribute();
 0187                declaringPropertyType = attr.GetType().BaseType;
 188            }
 189
 6190            string tmpStr = GetProperty<string>(attr, declaringPropertyType, nameof(MessageHeaderAttribute.Actor));
 6191            if (!string.IsNullOrEmpty(tmpStr))
 192            {
 0193                messageHeader.Actor = tmpStr;
 194            }
 6195            tmpStr = GetProperty<string>(attr, declaringPropertyType, nameof(MessageHeaderAttribute.Name));
 6196            if (!string.IsNullOrEmpty(tmpStr))
 197            {
 3198                messageHeader.Name = tmpStr;
 199            }
 6200            tmpStr = GetProperty<string>(attr, declaringPropertyType, nameof(MessageHeaderAttribute.Namespace));
 6201            if (!string.IsNullOrEmpty(tmpStr))
 202            {
 6203                messageHeader.Namespace = tmpStr;
 204            }
 6205            messageHeader.MustUnderstand = GetProperty<bool>(attr, declaringPropertyType, nameof(MessageHeaderAttribute.
 6206            messageHeader.Relay = GetProperty<bool>(attr, nameof(MessageHeaderAttribute.Relay));
 6207            return messageHeader;
 208        }
 209
 210        private static MessageBodyMemberAttribute ConvertFromServiceModelMessageBodyMemberAttribute(object attr)
 211        {
 212            Fx.Assert(attr.GetType().FullName.Equals(ServiceReflector.SMMessageBodyMemberAttributeFullName), "Expected a
 8213            var messageBody = new MessageBodyMemberAttribute();
 8214            string tmpStr = GetProperty<string>(attr, nameof(MessageBodyMemberAttribute.Name));
 8215            if (!string.IsNullOrEmpty(tmpStr))
 216            {
 0217                messageBody.Name = tmpStr;
 218            }
 8219            tmpStr = GetProperty<string>(attr, nameof(MessageBodyMemberAttribute.Namespace));
 8220            if (!string.IsNullOrEmpty(tmpStr))
 221            {
 6222                messageBody.Namespace = tmpStr;
 223            }
 8224            int order = GetProperty<int>(attr, nameof(MessageBodyMemberAttribute.Order));
 8225            if (order >= 0)
 226            {
 0227                messageBody.Order = order;
 228            }
 8229            return messageBody;
 230        }
 231
 232        private static MessagePropertyAttribute ConvertFromServiceModelMessagePropertyAttribute(object attr)
 233        {
 234            Fx.Assert(attr.GetType().FullName.Equals(ServiceReflector.SMMessagePropertyAttributeFullName), "Expected att
 3235            var messageProperty = new MessagePropertyAttribute();
 3236            string tmpStr = GetProperty<string>(attr, nameof(MessagePropertyAttribute.Name));
 3237            if (!string.IsNullOrEmpty(tmpStr))
 238            {
 1239                messageProperty.Name = tmpStr;
 240            }
 241
 3242            return messageProperty;
 243        }
 244
 245        private static MessageParameterAttribute ConvertFromServiceModelMessageParameterAttribute(object attr)
 246        {
 247            Fx.Assert(attr.GetType().FullName.Equals(ServiceReflector.SMMessageParameterAttributeFullName), "Expected at
 28248            var messageParameter = new MessageParameterAttribute();
 28249            string tmpStr = GetProperty<string>(attr, nameof(MessageParameterAttribute.Name));
 28250            if (!string.IsNullOrEmpty(tmpStr))
 251            {
 28252                messageParameter.Name = tmpStr;
 253            }
 254
 28255            return messageParameter;
 256        }
 257
 258        private static XmlSerializerFormatAttribute ConvertFromServiceModelXmlSerializerFormatAttribute(object attr)
 259        {
 260            Fx.Assert(attr.GetType().FullName.Equals(ServiceReflector.SMXmlSerializerFormatAttributeFullName), "Expected
 80261            var xmlSerializerFormatAttribute = new XmlSerializerFormatAttribute();
 262
 80263            xmlSerializerFormatAttribute.Style = GetProperty<OperationFormatStyle>(attr, nameof(XmlSerializerFormatAttri
 80264            xmlSerializerFormatAttribute.SupportFaults = GetProperty<bool>(attr, nameof(XmlSerializerFormatAttribute.Sup
 80265            xmlSerializerFormatAttribute.Use = GetProperty<OperationFormatUse>(attr, nameof(XmlSerializerFormatAttribute
 266
 80267            return xmlSerializerFormatAttribute;
 268        }
 269
 270        public static FaultContractAttribute ConvertFromServiceModelFaultContractAttribute(object attr)
 271        {
 272            Fx.Assert(attr.GetType().FullName.Equals(ServiceReflector.SMFaultContractAttributeFullName), "Expected attri
 4273            bool hasProtectionLevel = GetProperty<bool>(attr, "HasProtectionLevel");
 4274            if (hasProtectionLevel)
 275            {
 276                // ProtectionLevel isn't supported yet so if it was set on the S.SM.SCA, then we can't do the mapping so
 0277                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new PlatformNotSupportedException("System.Serv
 278            }
 279
 4280            var detailType = GetProperty<Type>(attr, nameof(FaultContractAttribute.DetailType));
 4281            var faultContractAttribute = new FaultContractAttribute(detailType);
 282
 4283            string tmpStr = GetProperty<string>(attr, nameof(FaultContractAttribute.Action));
 4284            if (!string.IsNullOrEmpty(tmpStr))
 285            {
 0286                faultContractAttribute.Action = tmpStr;
 287            }
 4288            tmpStr = GetProperty<string>(attr, nameof(FaultContractAttribute.Name));
 4289            if (!string.IsNullOrEmpty(tmpStr))
 290            {
 4291                faultContractAttribute.Name = tmpStr;
 292            }
 4293            tmpStr = GetProperty<string>(attr, nameof(FaultContractAttribute.Namespace));
 4294            if (!string.IsNullOrEmpty(tmpStr))
 295            {
 2296                faultContractAttribute.Namespace = tmpStr;
 297            }
 298
 4299            return faultContractAttribute;
 300        }
 301
 302        private static OperationContractAttribute ConvertFromServiceModelOperationContractAttribute(object attr)
 303        {
 304            Fx.Assert(attr.GetType().FullName.Equals(ServiceReflector.SMOperationContractAttributeFullName), "Expected a
 191305            bool hasProtectionLevel = GetProperty<bool>(attr, "HasProtectionLevel");
 191306            if (hasProtectionLevel)
 307            {
 308                // ProtectionLevel isn't supported yet so if it was set on the S.SM.SCA, then we can't do the mapping so
 0309                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new PlatformNotSupportedException("System.Serv
 310            }
 311
 191312            var oca = new OperationContractAttribute();
 191313            string tmpStr = GetProperty<string>(attr, nameof(OperationContractAttribute.Name));
 191314            if (!string.IsNullOrEmpty(tmpStr))
 315            {
 78316                oca.Name = tmpStr;
 317            }
 318
 191319            tmpStr = GetProperty<string>(attr, nameof(OperationContractAttribute.Action));
 191320            if (tmpStr != null) // String.Empty apparently is fine
 321            {
 78322                oca.Action = tmpStr;
 323            }
 324
 191325            tmpStr = GetProperty<string>(attr, nameof(OperationContractAttribute.ReplyAction));
 191326            if (tmpStr != null) // String.Empty apparently is fine
 327            {
 78328                oca.ReplyAction = tmpStr;
 329            }
 330
 191331            oca.AsyncPattern = GetProperty<bool>(attr, nameof(OperationContractAttribute.AsyncPattern));
 191332            oca.IsOneWay = GetProperty<bool>(attr, nameof(OperationContractAttribute.IsOneWay));
 191333            oca.IsInitiating = GetProperty<bool>(attr, nameof(OperationContractAttribute.IsInitiating));
 191334            oca.IsTerminating = GetProperty<bool>(attr, nameof(OperationContractAttribute.IsTerminating));
 191335            return oca;
 336        }
 337
 338        private static ServiceKnownTypeAttribute ConvertFromServiceModelServiceKnownTypeAttribute(object attr)
 339        {
 340            Fx.Assert(attr.GetType().FullName.Equals(ServiceReflector.SMServiceKnownTypeAttributeFullName), "Expected at
 18341            var skta = new ServiceKnownTypeAttribute();
 342
 18343            skta.MethodName= GetProperty<string>(attr, nameof(ServiceKnownTypeAttribute.MethodName));
 18344            skta.Type = GetProperty<Type>(attr, nameof(ServiceKnownTypeAttribute.Type));
 18345            skta.DeclaringType = GetProperty<Type>(attr, nameof(ServiceKnownTypeAttribute.DeclaringType));
 346
 18347            return skta;
 348        }
 349
 350        private static TProp GetProperty<TProp>(object obj, string propName)
 351        {
 352            Fx.Assert(obj != null, "Expected non-null object");
 3298353            return GetProperty<TProp>(obj, obj.GetType(), propName);
 354        }
 355
 356        private static TProp GetProperty<TProp>(object obj, Type declaringPropertyType, string propName)
 357        {
 358            Fx.Assert(obj != null, "Expected non-null object");
 359            PropertyInfo propInfo;
 3322360            if (typeof(TProp).IsEnum)
 361            {
 386362                propInfo = declaringPropertyType.GetProperty(propName);
 363                Fx.Assert(propInfo != null, $"Could not find property with name {propName} on object of type {obj.GetTyp
 386364                return (TProp)Enum.ToObject(typeof(TProp), propInfo.GetValue(obj));
 365            }
 366            else
 367            {
 2936368                propInfo = declaringPropertyType.GetProperty(propName, typeof(TProp));
 369                Fx.Assert(propInfo != null, $"Could not find property with name {propName} of type {typeof(TProp).FullNa
 2936370                return (TProp)propInfo.GetValue(obj);
 371            }
 372        }
 373    }
 374}