/// <summary>Activate the EmbeddedResourceAssemblyLoader.</summary>
 /// <returns>An old or new instance.</returns>
 public static EmbeddedResourceAssemblyLoader Activate()
 {
     if ((object)_ActiveInstance != null)
     {
         return(_ActiveInstance);
     }
     // create a new one
     {
         var assemblyLoader = new EmbeddedResourceAssemblyLoader();
         EmbeddedResourceAssemblyLoader._ActiveInstance = assemblyLoader;
         assemblyLoader.Wire();
         return(assemblyLoader);
     }
 }
 /// <summary>Deactivate the EmbeddedResourceAssemblyLoader.</summary>
 public static void Deactivate()
 {
     using (EmbeddedResourceAssemblyLoader._ActiveInstance) {
         EmbeddedResourceAssemblyLoader._ActiveInstance = null;
     }
 }
        private void ScanAssembly(System.Reflection.Assembly assembly)
        {
            var name          = assembly.GetName();
            var assembly_Name = name.Name;

            if (assembly_Name.StartsWith("System."))
            {
                return;
            }
            if (assembly_Name.StartsWith("Microsoft."))
            {
                return;
            }
            var publicKeyToken = name.GetPublicKeyToken();

            if (publicKeyToken != null)
            {
                if ((publicKeyToken[0] == 0xb7) &&
                    (publicKeyToken[1] == 0x7a) &&
                    (publicKeyToken[2] == 0x5c) &&
                    (publicKeyToken[3] == 0x56) &&
                    (publicKeyToken[4] == 0x19) &&
                    (publicKeyToken[5] == 0x34) &&
                    (publicKeyToken[6] == 0xe0) &&
                    (publicKeyToken[7] == 0x89))
                {
                    return;
                }

                if ((publicKeyToken[0] == 0xb0) &&
                    (publicKeyToken[1] == 0x3f) &&
                    (publicKeyToken[2] == 0x5f) &&
                    (publicKeyToken[3] == 0x7f) &&
                    (publicKeyToken[4] == 0x11) &&
                    (publicKeyToken[5] == 0xd5) &&
                    (publicKeyToken[6] == 0x0a) &&
                    (publicKeyToken[7] == 0x3a))
                {
                    return;
                }
            }

            var  hefezopfContractsAssemblyName = typeof(FuncstructorConfigurationAttribute).Assembly.GetName().Name;
            bool found = false;

            foreach (var referencedAssemblyName in assembly.GetReferencedAssemblies())
            {
                if (string.Equals(referencedAssemblyName.Name, hefezopfContractsAssemblyName, StringComparison.OrdinalIgnoreCase))
                {
                    found = true;
                }
            }

            if (!found)
            {
                return;
            }

            // look for FuncstructorConfigurationAttribute
            {
                var attrs = assembly.GetCustomAttributes <FuncstructorConfigurationAttribute>();
                foreach (var attr in attrs)
                {
                    var configurationType = attr.ConfigurationType;
                    var constructor       = configurationType.GetConstructor(Type.EmptyTypes);
                    var instance          = (IFuncstructorConfiguration)constructor.Invoke(new object[0]);
                    this._FuncstructorConfigurations.Add(instance);
                }
            }

            // look for FuncstructorFactoryAttribute
            {
                var attrs = assembly.GetCustomAttributes <FuncstructorFactoryAttribute>();
                foreach (var attr in attrs)
                {
                    var factoryType = attr.FactoryType;
                    var methods     = factoryType.GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
                    foreach (var method in methods)
                    {
                        var methodAttrs = method.GetCustomAttributes <FuncstructorRegisterAttribute>(false);
                        foreach (var methodAttr in methodAttrs)
                        {
                            // method.Invoke(null, parameters)
                            this._FuncstructorConfigurations.Add(new MethodFuncstructorConfiguration(method, methodAttr.Key));
                        }
                    }
                }
            }

            // look for AssemblyInjectionAttribute
            {
                var attrs = assembly.GetCustomAttributes <AssemblyInjectionAttribute>();
                foreach (var attr in attrs)
                {
                    EmbeddedResourceAssemblyLoader.Activate().RegisterAssembly(
                        attr.EmbeddedResource,
                        attr.AssemblyName,
                        assembly);
                }
            }
        }