Esempio n. 1
0
        /// <summary>
        /// Gets the location of a source file that contains the declaration of a method, or
        /// unknown if not available.
        /// </summary>
        /// <param name="method">The method.</param>
        /// <returns>The source location, or <see cref="CodeLocation.Unknown" /> if unknown.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="method"/> is null.</exception>
        public static CodeLocation GetSourceLocation(MethodBase method)
        {
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }

            Assembly assembly     = method.DeclaringType.Assembly;
            string   assemblyPath = AssemblyUtils.GetAssemblyLocation(assembly); // use the shadow-copied location if applicable

            if (assemblyPath == null)
            {
                return(CodeLocation.Unknown);
            }

            if (AppDomain.CurrentDomain.ShadowCopyFiles)
            {
                try
                {
                    string pdbPath = Path.ChangeExtension(assemblyPath, @".pdb");
                    if (!File.Exists(pdbPath))
                    {
                        string originalAssemblyPath = AssemblyUtils.GetAssemblyLocalPath(assembly);
                        if (originalAssemblyPath == null)
                        {
                            return(CodeLocation.Unknown);
                        }

                        string originalPdbPath = Path.ChangeExtension(originalAssemblyPath, @".pdb");
                        if (!File.Exists(originalPdbPath))
                        {
                            return(CodeLocation.Unknown);
                        }

                        File.Copy(originalPdbPath, pdbPath);
                    }
                }
                catch (IOException)
                {
                    return(CodeLocation.Unknown);
                }
            }

            return(Resolver.GetSourceLocationForMethod(assemblyPath, method.MetadataToken));
        }