GetApplicationType() public static method

Gets the type of the currently executing application.
public static GetApplicationType ( ) : ApplicationType
return ApplicationType
Ejemplo n.º 1
0
        public static List <Type> LoadImplementations(this Type type, string binariesDirectory, bool excludeAbstractTypes, bool validateReferences = true)
        {
            Assembly    asm;
            List <Type> types = new List <Type>();

            if (string.IsNullOrEmpty(binariesDirectory))
            {
                switch (Common.GetApplicationType())
                {
                // The binaries directory is not specified.
                case ApplicationType.Web:
                    // Use the bin directory for web applications.
                    binariesDirectory = FilePath.GetAbsolutePath($"bin{Path.DirectorySeparatorChar}*.*");
                    break;

                default:
                    // Use application install directory for application.
                    binariesDirectory = FilePath.GetAbsolutePath("*.*");
                    break;
                }
            }

            // Loop through all files in the binaries directory.
            foreach (string bin in FilePath.GetFileList(binariesDirectory))
            {
                // Only process DLLs and EXEs.
                if (!(string.Compare(FilePath.GetExtension(bin), ".dll", StringComparison.OrdinalIgnoreCase) == 0 ||
                      string.Compare(FilePath.GetExtension(bin), ".exe", StringComparison.OrdinalIgnoreCase) == 0))
                {
                    continue;
                }

                try
                {
                    // Load the assembly in the current app domain.
                    asm = Assembly.LoadFrom(bin);

                    if (!validateReferences || asm.TryLoadAllReferences())
                    {
                        // Process only the public types in the assembly.
                        foreach (Type asmType in asm.GetExportedTypes())
                        {
                            if (!excludeAbstractTypes || !asmType.IsAbstract)
                            {
                                // Either the current type is not abstract or it's OK to include abstract types.
                                if (type.IsClass && asmType.IsSubclassOf(type))
                                {
                                    // The type being tested is a class and current type derives from it.
                                    types.Add(asmType);
                                }

                                if (type.IsInterface && (object)asmType.GetInterface(type.FullName) != null)
                                {
                                    // The type being tested is an interface and current type implements it.
                                    types.Add(asmType);
                                }

                                if (type.GetRootType() == typeof(Attribute) && asmType.GetCustomAttributes(type, true).Length > 0)
                                {
                                    // The type being tested is an attribute and current type has the attribute.
                                    types.Add(asmType);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.SwallowException(ex, "TypeExtensions.cs LoadImplementations: Failed to load for some reason.");

                    // Absorb any exception thrown while processing the assembly.
                }
            }

            return(types);   // Return the matching types found.
        }
Ejemplo n.º 2
0
        public static List <Type> LoadImplementations(this Type type, string binariesDirectory, bool excludeAbstractTypes, bool validateReferences = true, bool executeStaticConstructors = false)
        {
            List <Type> types = new List <Type>();

            if (string.IsNullOrEmpty(binariesDirectory))
            {
                binariesDirectory = Common.GetApplicationType() switch
                {
                    // Use the bin directory for web applications.
                    ApplicationType.Web => FilePath.GetAbsolutePath($"bin{Path.DirectorySeparatorChar}*.*"),
                    _ => FilePath.GetAbsolutePath("*.*")
                };
            }

            using (Logger.SuppressFirstChanceExceptionLogMessages())
            {
                // Loop through all files in the binaries directory.
                foreach (string bin in FilePath.GetFileList(binariesDirectory))
                {
                    // Only process DLLs and EXEs.
                    if (!(string.Compare(FilePath.GetExtension(bin), ".dll", StringComparison.OrdinalIgnoreCase) == 0 ||
                          string.Compare(FilePath.GetExtension(bin), ".exe", StringComparison.OrdinalIgnoreCase) == 0))
                    {
                        continue;
                    }

                    try
                    {
                        // Load the assembly in the current app domain.
                        Assembly asm = Assembly.LoadFrom(bin);

                        if (!validateReferences || asm.TryLoadAllReferences())
                        {
                            // Process only the public types in the assembly.
                            foreach (Type asmType in asm.GetExportedTypes())
                            {
                                if (!excludeAbstractTypes || !asmType.IsAbstract)
                                {
                                    // Either the current type is not abstract or it's OK to include abstract types.
                                    if (type.IsClass && asmType.IsSubclassOf(type))
                                    {
                                        // The type being tested is a class and current type derives from it.
                                        types.Add(asmType);
                                    }

                                    if (type.IsInterface && (object)asmType.GetInterface(type.FullName) != null)
                                    {
                                        // The type being tested is an interface and current type implements it.
                                        types.Add(asmType);
                                    }

                                    if (type.GetRootType() == typeof(Attribute) && asmType.GetCustomAttributes(type, true).Length > 0)
                                    {
                                        // The type being tested is an attribute and current type has the attribute.
                                        types.Add(asmType);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.SwallowException(ex, $"TypeExtensions.cs LoadImplementations: Failed to load DLL \"{bin}\", may not be a managed assembly.");
                    }
                }

                if (executeStaticConstructors)
                {
                    ThreadPool.QueueUserWorkItem(state =>
                    {
                        if (state is not Type[] asmTypes)
                        {
                            return;
                        }

                        // Make sure static constructor is executed for each loaded type
                        foreach (Type asmType in asmTypes)
                        {
                            try
                            {
                                RuntimeHelpers.RunClassConstructor(asmType.TypeHandle);
                            }
                            catch (Exception ex)
                            {
                                Logger.SwallowException(ex, $"TypeExtensions.cs LoadImplementations: Failed to run static constructor for \"{asmType.FullName}\": {ex.Message}");
                            }
                        }
                    },
                                                 types.ToArray());
                }
            }

            return(types);   // Return the matching types found.
        }
    }
Ejemplo n.º 3
0
        /// <summary>
        /// Loads public types from assemblies in the specified <paramref name="binariesDirectory"/> that implement
        /// the specified <paramref name="type"/> either through class inheritance or interface implementation.
        /// </summary>
        /// <param name="type">The <see cref="Type"/> that must be implemented by the public types.</param>
        /// <param name="binariesDirectory">The directory containing the assemblies to be processed.</param>
        /// <param name="excludeAbstractTypes">true to exclude public types that are abstract; otherwise false.</param>
        /// <returns>Public types that implement the specified <paramref name="type"/>.</returns>
        public static List <Type> LoadImplementations(this Type type, string binariesDirectory, bool excludeAbstractTypes)
        {
            Assembly    asm   = null;
            List <Type> types = new List <Type>();

            if (string.IsNullOrEmpty(binariesDirectory))
            {
                switch (Common.GetApplicationType())
                {
                // The binaries directory is not specified.
                case ApplicationType.WindowsGui:
                case ApplicationType.WindowsCui:
                    // Use application install directory for windows applications.
                    binariesDirectory = FilePath.GetAbsolutePath("*.*");
                    break;

                case ApplicationType.Web:
                    // Use the bin directory for web applications.
                    binariesDirectory = FilePath.GetAbsolutePath("bin\\*.*");
                    break;
                }
            }

            // Loop through all files in the binaries directory.
            foreach (string bin in FilePath.GetFileList(binariesDirectory))
            {
                // Only process DLLs and EXEs.
                if (!(string.Compare(FilePath.GetExtension(bin), ".dll", true) == 0 ||
                      string.Compare(FilePath.GetExtension(bin), ".exe", true) == 0))
                {
                    continue;
                }

                try
                {
                    // Load the assembly in the curent app domain.
                    asm = Assembly.LoadFrom(bin);

                    // Process only the public types in the assembly.
                    foreach (Type asmType in asm.GetExportedTypes())
                    {
                        if (!excludeAbstractTypes || !asmType.IsAbstract)
                        {
                            // Either the current type is not abstract or it's OK to include abstract types.
                            if (type.IsClass && asmType.IsSubclassOf(type))
                            {
                                // The type being tested is a class and current type derives from it.
                                types.Add(asmType);
                            }

                            if (type.IsInterface && (object)asmType.GetInterface(type.Name) != null)
                            {
                                // The type being tested is an interface and current type implements it.
                                types.Add(asmType);
                            }

                            if (type.GetRootType() == typeof(Attribute) && asmType.GetCustomAttributes(type, true).Length > 0)
                            {
                                // The type being tested is an attribute and current type has the attribute.
                                types.Add(asmType);
                            }
                        }
                    }
                }
                catch
                {
                    // Absorb any exception thrown while processing the assembly.
                }
            }

            return(types);   // Return the matching types found.
        }