private IgnoringExceptionsAssemblyResolver CreateAssemblyResolver(IEnumerable <ResolvedAllowedAssemblyReference> resolvedAllowedAssemblyReferences)
        {
            IgnoringExceptionsAssemblyResolver assemblyResolver = new IgnoringExceptionsAssemblyResolver();

            assemblyResolver.ResolveFailure += (sender, reference) => {
                foreach (ResolvedAllowedAssemblyReference resolvedAllowedAssemblyReference in resolvedAllowedAssemblyReferences)
                {
                    if (String.IsNullOrWhiteSpace(resolvedAllowedAssemblyReference.Path))
                    {
                        continue;
                    }

                    if (!reference.IsAssemblyReferencesMatch(
                            resolvedAllowedAssemblyReference.AssemblyNameReference,
                            resolvedAllowedAssemblyReference.StrictNameCheck))
                    {
                        continue;
                    }

                    Log.DebugFormat(
                        "Resolving referenced assembly {0} at path '{1}'",
                        resolvedAllowedAssemblyReference.AssemblyNameReference,
                        resolvedAllowedAssemblyReference.Path
                        );

                    AssemblyDefinition resolvedAssembly =
                        GetAssemblyDefinitionCachedData(resolvedAllowedAssemblyReference.Path, assemblyResolver).AssemblyDefinition;
                    return(resolvedAssembly);
                }
                return(null);
            };
            return(assemblyResolver);
        }
        public ResolvedInjectionConfiguration Load()
        {
            try {
                Log.Debug("Calculating list of injectee assemblies");
                List <ResolvedInjecteeAssembly> injecteeAssemblies = GetInjecteeAssemblies();

                // Construct compound assembly resolver that uses compound list of allowed assembly references,
                // which will be used when resolving assemblies used in injected methods
                IEnumerable <ResolvedAllowedAssemblyReference> compoundResolvedAllowedAssemblyReferences =
                    injecteeAssemblies
                    .SelectMany(injecteeAssembly => injecteeAssembly.AllowedAssemblyReferences)
                    .ToArray();

                _compoundAssemblyResolver = CreateAssemblyResolver(compoundResolvedAllowedAssemblyReferences);

                Log.Debug("Calculating filtered list of injected methods");
                Dictionary <AssemblyDefinition, List <ResolvedInjectedMethod> > injectedAssemblyToMethodsMap = GetInjectedMethods();

                List <ResolvedInjectedMethod> injectedMethods = new List <ResolvedInjectedMethod>();
                foreach (KeyValuePair <AssemblyDefinition, List <ResolvedInjectedMethod> > pair in injectedAssemblyToMethodsMap)
                {
                    injectedMethods.AddRange(pair.Value);
                }

                // Validation
                Validate(injectedMethods, injecteeAssemblies);

                ResolvedInjectionConfiguration resolvedInjectionConfiguration =
                    new ResolvedInjectionConfiguration(
                        injectedMethods,
                        injecteeAssemblies
                        );
                return(resolvedInjectionConfiguration);
            } catch (Exception e) {
                throw new MethodInlineInjectorException("Unknown exception", e);
            }
        }
        private ResolvedInjecteeAssembly GetInjecteeAssembly(InjecteeAssembly sourceInjecteeAssembly)
        {
            List <IgnoredMemberReference>   ignoredMemberReferences   = new List <IgnoredMemberReference>();
            List <AllowedAssemblyReference> allowedAssemblyReferences = new List <AllowedAssemblyReference>();

            void LoadIgnoredMemberReferences(IEnumerable <IIgnoredMemberReference> items)
            {
                foreach (IIgnoredMemberReference item in items)
                {
                    if (item is IgnoredMemberReference ignoredMemberReference)
                    {
                        ignoredMemberReferences.Add(ignoredMemberReference);
                        continue;
                    }

                    if (item is IgnoredMemberReferenceInclude ignoredMemberReferenceInclude)
                    {
                        try {
                            Log.DebugFormat("Loading ignored member references list include at '{0}'", ignoredMemberReferenceInclude.Path);
                            string includeXml = File.ReadAllText(ignoredMemberReferenceInclude.Path);
                            IgnoredMemberReferencesIncludeLoader ignoredMemberReferencesIncludeLoader =
                                SimpleXmlSerializationUtility.XmlDeserializeFromString <IgnoredMemberReferencesIncludeLoader>(includeXml);
                            LoadIgnoredMemberReferences(ignoredMemberReferencesIncludeLoader.Items);
                        } catch (Exception e) {
                            Console.WriteLine(e);
                            throw new MethodInlineInjectorException(
                                      $"Unable to load ignored member references list include at '{ignoredMemberReferenceInclude.Path}'",
                                      e
                                      );
                        }
                    }
                }
            }

            void LoadAllowedAssemblyReferences(IEnumerable <IAllowedAssemblyReference> items)
            {
                foreach (IAllowedAssemblyReference item in items)
                {
                    if (item is AllowedAssemblyReference allowedAssemblyReference)
                    {
                        allowedAssemblyReferences.Add(allowedAssemblyReference);
                        continue;
                    }

                    if (item is AllowedAssemblyReferenceInclude allowedAssemblyReferenceInclude)
                    {
                        Log.DebugFormat("Loading allowed assembly references list include at '{0}'", allowedAssemblyReferenceInclude.Path);
                        try {
                            string includeXml = File.ReadAllText(allowedAssemblyReferenceInclude.Path);
                            AllowedAssemblyReferenceIncludeLoader allowedAssemblyReferencesLoader =
                                SimpleXmlSerializationUtility.XmlDeserializeFromString <AllowedAssemblyReferenceIncludeLoader>(includeXml);
                            LoadAllowedAssemblyReferences(allowedAssemblyReferencesLoader.Items);
                        } catch (Exception e) {
                            throw new MethodInlineInjectorException(
                                      $"Unable to load allowed assembly references list include at '{allowedAssemblyReferenceInclude.Path}'",
                                      e
                                      );
                        }
                    }
                }
            }

            LoadIgnoredMemberReferences(sourceInjecteeAssembly.IgnoredMemberReferences);
            LoadAllowedAssemblyReferences(sourceInjecteeAssembly.AllowedAssemblyReferences);

            List <ResolvedAllowedAssemblyReference> resolvedAllowedAssemblyReferences =
                allowedAssemblyReferences
                .Select(item => new ResolvedAllowedAssemblyReference(AssemblyNameReference.Parse(item.Name), item.Path, item.StrictNameCheck))
                .ToList();

            // Configure assembly resolver for this assembly
            IgnoringExceptionsAssemblyResolver assemblyResolver             = CreateAssemblyResolver(resolvedAllowedAssemblyReferences);
            AssemblyDefinitionCachedData       assemblyDefinitionCachedData =
                GetAssemblyDefinitionCachedData(sourceInjecteeAssembly.AssemblyPath, assemblyResolver);

            Log.DebugFormat(
                "Calculating injectee methods in assembly '{0}'",
                assemblyDefinitionCachedData.AssemblyDefinition.MainModule.FullyQualifiedName);

            List <MethodDefinition> filteredInjecteeMethods =
                GetFilteredInjecteeMethods(assemblyDefinitionCachedData, ignoredMemberReferences);

            ResolvedInjecteeAssembly resolvedInjecteeAssembly =
                new ResolvedInjecteeAssembly(
                    assemblyDefinitionCachedData.AssemblyDefinition,
                    filteredInjecteeMethods,
                    resolvedAllowedAssemblyReferences,
                    assemblyResolver
                    );

            return(resolvedInjecteeAssembly);
        }