Ejemplo n.º 1
0
        public virtual bool ShouldBeAsynchronous(MethodInfo method, bool force)
        {
            if (method.IsAsyncMethod())
            {
                return(false);
            }

            List <MethodInfo> methods = ContractDefinition.GetEffectiveMethods(method.DeclaringType).ToList();

            if (force)
            {
                return(methods.All(m => m.Name != method.Name + AsyncSuffix));
            }

            if (!method.CustomAttributes.Any())
            {
                return(false);
            }

            var found = method.CustomAttributes.FirstOrDefault(a => a.AttributeType.Name == BoltConstants.Core.AsyncOperationAttribute.Name);

            if (found == null)
            {
                return(false);
            }

            return(methods.All(m => m.Name != method.Name + AsyncSuffix));
        }
Ejemplo n.º 2
0
        private bool ShouldHaveAsyncMethods(Type contract)
        {
            if (ForceAsynchronous)
            {
                return(true);
            }

            return(ContractDefinition.GetEffectiveMethods(contract).Any(m => ShouldBeAsynchronous(m, ForceAsynchronous)));
        }
Ejemplo n.º 3
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);
        }