Ejemplo n.º 1
0
        public CciQueryAssembly(CciAssembly inputAssembly, ITypeDefinition typeToAnalyze, IReadOnlyCollection <Query> queries)
            : base(inputAssembly.Module, inputAssembly.ContractProvider)
        {
            Contract.Requires(inputAssembly != null);
            Contract.Requires(typeToAnalyze != null);
            Contract.Requires(queries.Any());

            // Clone module
            var host = CciHostEnvironment.GetInstance();

            module = new CodeAndContractDeepCopier(host).Copy(inputAssembly.Module);
            var cciQueryType = FindType(module, typeToAnalyze.Name);

            Contract.Assert(cciQueryType != null);

            // Create contract provider for the cloned module
            contractProvider = new ContractProvider(new ContractMethods(host), host.FindUnit(module.UnitIdentity));

            var cciInputType = FindType(inputAssembly.Module, typeToAnalyze.Name);

            Contract.Assert(cciInputType != null);
            var queryTypeContract = inputAssembly.ContractProvider.GetTypeContractFor(cciInputType);

            contractProvider.AssociateTypeWithContract(cciQueryType, queryTypeContract);

            // Add queries
            cciQueryType.Methods.AddRange(from a in queries select a.Method.Method);

            foreach (var query in queries)
            {
                // Find the query in the query assembly
                var method = cciQueryType.Methods.Find(m => m.GetUniqueName().Equals(query.Method.Name)) as MethodDefinition;
                Contract.Assert(method != null);

                method.ContainingTypeDefinition = cciQueryType;

                // Asociate query with its contract
                contractProvider.AssociateMethodWithContract(query.Method.Method, query.Method.Contract);
            }
        }