static internal object[] GetCustomAttributes(CustomAttributeProvider attrProvider, Type attrType, bool inherit) { try { return(attrProvider.GetCustomAttributes(attrType, inherit) ?? Array.Empty <object>()); } catch (Exception e) { if (Fx.IsFatal(e)) { throw; } // where the exception is CustomAttributeFormatException and the InnerException is a TargetInvocationException, // drill into the InnerException as this will provide a better error experience (fewer nested InnerExceptions) // CustomerAttributeFormatException only exists from .Net Standard 1.7, so using base type FormatException instead. if (e is FormatException && e.InnerException != null) { e = e.InnerException; if (e is TargetInvocationException && e.InnerException != null) { e = e.InnerException; } } TypeInfo typeInfo = attrProvider.TypeInfo; MethodInfo method = attrProvider.MethodInfo; //ParameterInfo param = attrProvider as ParameterInfo; // there is no good way to know if this is a return type attribute if (typeInfo != null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( SR.Format(SR.SFxErrorReflectingOnType2, attrType.Name, typeInfo.Name), e)); } else if (method != null) { // Changed ReflectedType to DeclaringType as ReflectedType isn't available until .Net standard 1.7 // TODO: Consider changing back if project is changed to target .Net standard 1.7 or later 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.ReflectedType.Name), e)); // } //} throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( SR.Format(SR.SFxErrorReflectionOnUnknown1, attrType.Name), e)); } }
internal static 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); }
internal static object[] GetCustomAttributes(CustomAttributeProvider attrProvider, Type attrType, bool inherit) { try { return(attrProvider.GetCustomAttributes(attrType, inherit) ?? Array.Empty <object>()); } catch (Exception e) { if (Fx.IsFatal(e)) { throw; } // where the exception is CustomAttributeFormatException and the InnerException is a TargetInvocationException, // drill into the InnerException as this will provide a better error experience (fewer nested InnerExceptions) if (e is CustomAttributeFormatException && e.InnerException != null) { e = e.InnerException; if (e is TargetInvocationException && e.InnerException != null) { e = e.InnerException; } } TypeInfo typeInfo = attrProvider.TypeInfo; MethodInfo method = attrProvider.MethodInfo; ParameterInfo param = attrProvider.ParameterInfo; // there is no good way to know if this is a return type attribute if (typeInfo != null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( SR.Format(SR.SFxErrorReflectingOnType2, attrType.Name, typeInfo.Name), e)); } else if (method != null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( SR.Format(SR.SFxErrorReflectingOnMethod3, attrType.Name, method.Name, method.ReflectedType.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.ReflectedType.Name), e)); } } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( SR.Format(SR.SFxErrorReflectionOnUnknown1, attrType.Name), e)); } }
internal MessagePartDescription(MessagePartDescription other) { name = other.name; ns = other.ns; index = other.index; type = other.type; serializationPosition = other.serializationPosition; this.hasProtectionLevel = other.hasProtectionLevel; this.protectionLevel = other.protectionLevel; memberInfo = other.memberInfo; multiple = other.multiple; additionalAttributesProvider = other.additionalAttributesProvider; //this.baseType = other.baseType; //this.uniquePartName = other.uniquePartName; }
internal static 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) { XmlName = other.XmlName; Namespace = other.Namespace; Index = other.Index; Type = other.Type; SerializationPosition = other.SerializationPosition; _hasProtectionLevel = other._hasProtectionLevel; _protectionLevel = other._protectionLevel; MemberInfo = other.MemberInfo; Multiple = other.Multiple; _additionalAttributesProvider = other._additionalAttributesProvider; //this.baseType = other.baseType; //this.uniquePartName = other.uniquePartName; }
internal static T GetSingleAttribute <T>(CustomAttributeProvider attrProvider) where T : class { Type attrType = typeof(T); object[] attrs = GetCustomAttributes(attrProvider, attrType); if (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); } }
internal static 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); }
internal static object[] GetCustomAttributes(CustomAttributeProvider attrProvider, Type attrType) { return(GetCustomAttributes(attrProvider, attrType, false)); }