Пример #1
0
        internal void LoadMethodProperties(Exception exception, System.Diagnostics.StackFrame stackFrame)
        {
            // Validation
            if (exception == null || exception.TargetSite == null || typeof(System.Reflection.MethodInfo).IsAssignableFrom(exception.TargetSite.GetType()) == false)
            {
                return;
            }

            // Get Reflection Information
            System.Reflection.MethodBase methodBase     = stackFrame.GetMethod();
            System.Reflection.MethodBody methodBody     = methodBase.GetMethodBody();
            System.Reflection.MethodInfo methodInfo     = (methodBase != null && typeof(System.Reflection.MethodInfo).IsAssignableFrom(methodBase.GetType()) == true) ? (System.Reflection.MethodInfo)methodBase : null;
            System.Reflection.MethodInfo targetSiteInfo = ((System.Reflection.MethodInfo)exception.TargetSite);

            // Get Return Information
            this.m_ReturnParameter = (targetSiteInfo != null && targetSiteInfo.ReturnParameter != null) ? targetSiteInfo.Name : "";
            this.m_ReturnType      = (targetSiteInfo != null && targetSiteInfo.ReturnType != null) ? targetSiteInfo.ReturnType.FullName : "";

            // Get Line Number
            int intLineNumber = (GetLineNumber(exception) == 0) ? stackFrame.GetFileLineNumber() : GetLineNumber(exception);

            this.m_LineNumber = (this.LineNumber == 0) ? stackFrame.GetFileLineNumber() : intLineNumber;

            if (methodInfo != null && methodInfo.GetParameters().Count() > 0)
            {
                this.m_Parameters = methodInfo.GetParameters().Select(param => new ParameterInformation(param.Name, param.ParameterType.ToString())).ToList();
            }
        }
Пример #2
0
        /// <summary>
        /// Check the method body of the Initialize method of an analyzer and if that's empty,
        /// then the analyzer hasn't been implemented yet.
        /// </summary>
        private static bool HasImplementation(DiagnosticAnalyzer analyzer)
        {
            System.Reflection.MethodInfo method = analyzer.GetType().GetMethod("Initialize");
            if (method != null)
            {
                System.Reflection.MethodBody body = method.GetMethodBody();
                int?ilInstructionCount            = body?.GetILAsByteArray()?.Count();
                // An empty method has two IL instructions - nop and ret.
                return(ilInstructionCount != 2);
            }

            return(true);
        }
Пример #3
0
        internal void LoadClassFileInformation(Exception exception, System.Diagnostics.StackFrame stackFrame)
        {
            // Get Reflection Information
            System.Reflection.MethodBase methodBase     = stackFrame.GetMethod();
            System.Reflection.MethodBody methodBody     = methodBase.GetMethodBody();
            System.Reflection.MethodInfo methodInfo     = (methodBase != null && typeof(System.Reflection.MethodInfo).IsAssignableFrom(methodBase.GetType()) == true) ? (System.Reflection.MethodInfo)methodBase : null;
            System.Reflection.MethodInfo targetSiteInfo = ((System.Reflection.MethodInfo)exception.TargetSite);

            // Load Method Properties
            this.Method.LoadMethodProperties(exception, stackFrame);

            this.m_Name     = (methodInfo != null && methodInfo.ReflectedType != null) ? methodInfo.ReflectedType.Name : "";
            this.m_FullName = methodBase.ReflectedType.FullName + " - " + this.Method.Name;
        }
Пример #4
0
        private void LoadError()
        {
            // Get Application Information
            this.m_Application = new ApplicationInformation();

            // Loop Stack Trace Frames
            System.Diagnostics.StackTrace CurrentStackTest = new System.Diagnostics.StackTrace(this.Exception, 0, true);
            for (int intIndex = 0; intIndex < CurrentStackTest.FrameCount; intIndex++)
            {
                // Get Reflection Information
                System.Diagnostics.StackFrame frame      = (CurrentStackTest != null && intIndex >= 0) ? CurrentStackTest.GetFrame(intIndex) : null;
                System.Reflection.MethodBase  methodBase = (frame != null) ? frame.GetMethod() : null;
                System.Reflection.MethodBody  methodBody = (methodBase != null) ? methodBase.GetMethodBody() : null;
                System.Reflection.MethodInfo  info       = (methodBase != null && typeof(System.Reflection.MethodInfo).IsAssignableFrom(methodBase.GetType()) == true) ? info = (System.Reflection.MethodInfo)methodBase : null;

                //// Validation
                if (this.m_Class != null && this.m_Class.Method.Name != System.Reflection.MethodBase.GetCurrentMethod().Name)
                {
                    // SET ATTEMPTED METHOD
                    System.Diagnostics.StackFrame previousFrame = (intIndex - 1 >= 0) ? CurrentStackTest.GetFrame(intIndex - 1) : null;
                    System.Reflection.MethodBase  previousBase  = (previousFrame != null) ? ((System.Reflection.MethodBase)previousFrame.GetMethod()) : null;

                    string strMethod = (previousBase != null && typeof(System.Reflection.MethodInfo).IsAssignableFrom(previousBase.GetType()) == true) ? previousBase.DeclaringType.FullName + " - " + ((System.Reflection.MethodInfo)previousBase).ToString() : info.Name;
                    this.m_Class.AttemptedMethod = (intIndex < 2 && strMethod != this.m_Class.Method.Name) ? strMethod : this.m_Class.AttemptedMethod;
                }

                // Validation
                if (intIndex > 2)
                {
                    continue;
                }

                // LOAD ASSEMBLY PROPERTIES
                this.m_Assembly = new AssemblyInformation(info);

                // LOAD METHOD PROPERTIES
                this.m_Class = new ClassFileInformation(this.Exception, frame);
            }
        }
Пример #5
0
            /// <summary>
            ///  Walk up the inheritance tree until we find an override
            /// or the class where the abstract method was defined
            /// </summary>
            private bool IsOverridden(TrustedMethodBase method, TrustedType type)
            {
                if (method.DeclaringType == type)
                {
                    return(false);
                }

                TrustedMethodInfo[] methods = type.GetMethods(flags);
                if (methods != null)
                {
                    foreach (TrustedMethodInfo info in methods)
                    {
                        if (MethodSignaturesMatch(info, method))
                        {
                            MethodBody body = info.GetMethodBody();
                            if (body != null)
                            {
                                return(true);
                            }
                        }
                    }
                }
                return(IsOverridden(method, type.BaseType));
            }