Exemple #1
0
        private void GenerateInterface(Type contract, object context, string name)
        {
            string suffix = InterfaceSuffix ?? AsyncSuffix;

            if (ExcludedInterfaces != null)
            {
                if (ExcludedInterfaces.Contains(contract.FullName) ||
                    ExcludedInterfaces.Contains(contract.FullName + suffix))
                {
                    return;
                }
            }

            if (_generatedInterfaces.Contains(contract))
            {
                return;
            }

            if (
                !ContractDefinition.GetEffectiveContracts(contract)
                .Concat(new[] { contract })
                .Any(ShouldGenerateInterface))
            {
                return;
            }

            _generatedInterfaces.Add(contract);
            name = name ?? (contract.Name + suffix);
            _generatedAsyncInterfaces.Add(name);

            List <string> asyncBase =
                ContractDefinition.GetEffectiveContracts(contract).Where(ShouldGenerateInterface).Where(
                    i =>
            {
                if (ExcludedInterfaces != null)
                {
                    if (ExcludedInterfaces.Contains(i.FullName))
                    {
                        return(false);
                    }
                }
                return(true);
            }).Select(t => FormatType(t) + suffix).ToList();

            asyncBase.Insert(0, FormatType(contract));

            ClassGenerator classGenerator =
                CreateClassGenerator(new ClassDescriptor(name, contract.Namespace, asyncBase.ToArray())
            {
                IsInterface = true
            });

            classGenerator.Modifier           = Modifier;
            classGenerator.GenerateBodyAction = g =>
            {
                var methods =
                    (from method in ContractDefinition.GetEffectiveMethods(contract)
                     let async = ShouldBeAsynchronous(method, ForceAsynchronous)
                                 let sync = ShouldBeSync(method, ForceSynchronous)
                                            where async || sync
                                            select new { method, async, sync }).ToList();

                foreach (var method in methods)
                {
                    if (method.async)
                    {
                        g.WriteLine(FormatMethodDeclaration(method.method, MethodDeclarationFormatting.ChangeToAsync) + ";");
                    }
                    else
                    {
                        g.WriteLine(FormatMethodDeclaration(method.method, MethodDeclarationFormatting.ChangeToSync) + ";");
                    }

                    if (!Equals(method, methods.Last()))
                    {
                        g.WriteLine();
                    }
                }
            };

            classGenerator.Generate(context);
        }