示例#1
0
        /// <summary>
        /// Try to get the directory this assembly is in. Returns null if assembly
        /// was in the GAC or DLL location can not be retrieved.
        /// </summary>
        private static string TryGetClientDir()
        {
            var buildTask = typeof(ManagedCompiler).GetTypeInfo().Assembly;

            var inGac = (bool?)typeof(Assembly)
                        .GetTypeInfo()
                        .GetDeclaredProperty("GlobalAssemblyCache")
                        ?.GetMethod.Invoke(buildTask, parameters: null);

            if (inGac != false)
            {
                return(null);
            }

            var codeBase = (string)typeof(Assembly)
                           .GetTypeInfo()
                           .GetDeclaredProperty("CodeBase")
                           ?.GetMethod.Invoke(buildTask, parameters: null);

            if (codeBase == null)
            {
                return(null);
            }

            var uri = new Uri(codeBase);

            string assemblyPath;

            if (uri.IsFile)
            {
                assemblyPath = uri.LocalPath;
            }
            else
            {
                var callingAssembly = (Assembly)typeof(Assembly)
                                      .GetTypeInfo()
                                      .GetDeclaredMethod("GetCallingAssembly")
                                      ?.Invoke(null, null);

                var location = Utilities.GetLocation(callingAssembly);
                if (location == null)
                {
                    return(null);
                }

                assemblyPath = location;
            }

            return(Path.GetDirectoryName(assemblyPath));
        }
示例#2
0
 private string GetDirectory(Assembly assembly) => Path.GetDirectoryName(Utilities.GetLocation(assembly));