Exemplo n.º 1
0
        static internal object[] GetCustomAttributes(CustomAttributeProvider attrProvider, Type attrType, bool inherit)
        {
            try
            {
                if (typeof(Attribute).IsAssignableFrom(attrType))
                {
                    return(attrProvider.GetCustomAttributes(attrType, inherit));
                }
                else
                {
                    List <object> attrTypeAttributes  = new List <object>();
                    object[]      allCustomAttributes = attrProvider.GetCustomAttributes(inherit);
                    foreach (object customAttribute in allCustomAttributes)
                    {
                        if (attrType.IsAssignableFrom(customAttribute.GetType()))
                        {
                            attrTypeAttributes.Add(customAttribute);
                        }
                    }

                    return(attrTypeAttributes.ToArray());
                }
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                Type          type   = attrProvider.Type;
                MethodInfo    method = attrProvider.MethodInfo;
                ParameterInfo param  = attrProvider.ParameterInfo;
                // there is no good way to know if this is a return type attribute
                if (type != null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
                                                                                  SR.Format(SR.SFxErrorReflectingOnType2, attrType.Name, type.Name), e));
                }
                else if (method != null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
                                                                                  SR.Format(SR.SFxErrorReflectingOnMethod3,
                                                                                            attrType.Name, method.Name, method.DeclaringType.Name), e));
                }
                else if (param != null)
                {
                    method = param.Member as MethodInfo;
                    if (method != null)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
                                                                                      SR.Format(SR.SFxErrorReflectingOnParameter4,
                                                                                                attrType.Name, param.Name, method.Name, method.DeclaringType.Name), e));
                    }
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
                                                                              SR.Format(SR.SFxErrorReflectionOnUnknown1, attrType.Name), e));
            }
        }
Exemplo n.º 2
0
        static internal T GetRequiredSingleAttribute <T>(CustomAttributeProvider attrProvider, Type[] attrTypeGroup)
            where T : class
        {
            T result = GetSingleAttribute <T>(attrProvider, attrTypeGroup);

            if (result == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.couldnTFindRequiredAttributeOfTypeOn2, typeof(T), attrProvider.ToString())));
            }
            return(result);
        }
Exemplo n.º 3
0
 internal MessagePartDescription(MessagePartDescription other)
 {
     _name  = other._name;
     _ns    = other._ns;
     _index = other._index;
     _type  = other._type;
     _serializationPosition = other._serializationPosition;
     _hasProtectionLevel    = other._hasProtectionLevel;
     _protectionLevel       = other._protectionLevel;
     _memberInfo            = other._memberInfo;
     _multiple = other._multiple;
     _additionalAttributesProvider = other._additionalAttributesProvider;
     _baseType       = other._baseType;
     _uniquePartName = other._uniquePartName;
 }
Exemplo n.º 4
0
        static internal T GetFirstAttribute <T>(CustomAttributeProvider attrProvider)
            where T : class
        {
            Type attrType = typeof(T);

            object[] attrs = GetCustomAttributes(attrProvider, attrType);
            if (attrs.Length == 0)
            {
                return(null);
            }
            else
            {
                return(attrs[0] as T);
            }
        }
 internal MessagePartDescription(MessagePartDescription other)
 {
     _name = other._name;
     _ns = other._ns;
     _index = other._index;
     _type = other._type;
     _serializationPosition = other._serializationPosition;
     _hasProtectionLevel = other._hasProtectionLevel;
     _protectionLevel = other._protectionLevel;
     _memberInfo = other._memberInfo;
     _multiple = other._multiple;
     _additionalAttributesProvider = other._additionalAttributesProvider;
     _baseType = other._baseType;
     _uniquePartName = other._uniquePartName;
 }
Exemplo n.º 6
0
        static internal T GetSingleAttribute <T>(CustomAttributeProvider attrProvider)
            where T : class
        {
            Type attrType = typeof(T);

            object[] attrs = GetCustomAttributes(attrProvider, attrType);
            if (attrs == null || attrs.Length == 0)
            {
                return(null);
            }
            else if (attrs.Length > 1)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.tooManyAttributesOfTypeOn2, attrType, attrProvider.ToString())));
            }
            else
            {
                return(attrs[0] as T);
            }
        }
Exemplo n.º 7
0
        static internal T GetSingleAttribute <T>(CustomAttributeProvider attrProvider, Type[] attrTypeGroup)
            where T : class
        {
            T result = GetSingleAttribute <T>(attrProvider);

            if (result != null)
            {
                Type attrType = typeof(T);
                foreach (Type otherType in attrTypeGroup)
                {
                    if (otherType == attrType)
                    {
                        continue;
                    }
                    object[] attrs = GetCustomAttributes(attrProvider, otherType);
                    if (attrs != null && attrs.Length > 0)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFxDisallowedAttributeCombination, attrProvider, attrType.FullName, otherType.FullName)));
                    }
                }
            }
            return(result);
        }
Exemplo n.º 8
0
 static internal object[] GetCustomAttributes(CustomAttributeProvider attrProvider, Type attrType)
 {
     return(GetCustomAttributes(attrProvider, attrType, false));
 }