Esempio n. 1
0
        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));
            }
        }
Esempio n. 2
0
        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));
            }
        }