public JimuServiceDesc Parse(MethodInfo methodInfo) { JimuServiceDesc desc = new JimuServiceDesc(); var descriptorAttributes = methodInfo.GetCustomAttributes <JimuServiceDescAttribute>(); foreach (var attr in descriptorAttributes) { attr.Apply(desc); } if (string.IsNullOrEmpty(desc.Comment)) { var xml = GetXmlComment(methodInfo.DeclaringType); var key = XmlCommentsMemberNameHelper.GetMemberNameForMethod(methodInfo); if (xml != null && xml.TryGetValue(key, out var node)) { var summaryNode = node.SelectSingleNode("summary"); if (summaryNode != null) { desc.Comment = summaryNode.Value.Trim(); } } } desc.ReturnDesc = GetReturnDesc(methodInfo); if (string.IsNullOrEmpty(desc.HttpMethod)) { desc.HttpMethod = GetHttpMethod(methodInfo); } desc.Parameters = JimuHelper.Serialize <string>(GetParameters(methodInfo)); if (string.IsNullOrEmpty(desc.Id)) { desc.Id = JimuHelper.GenerateServiceId(methodInfo); } var type = methodInfo.DeclaringType; var routeTemplate = type.GetCustomAttribute <JimuAttribute>(); if (routeTemplate != null) { if (string.IsNullOrEmpty(desc.RoutePath)) { var setPath = string.IsNullOrEmpty(desc.Rest) ? methodInfo.Name : desc.Rest; desc.ServiceClassPath = JimuServiceRoute.ParseClassPath(routeTemplate.GetTemplate(type), type.Name, type.IsInterface); desc.RoutePath = JimuServiceRoute.ParseRoutePath(desc.HttpMethod, desc.ServiceClassPath, setPath, methodInfo.GetParameters().Select(x => x.Name).ToArray(), type.IsInterface); } } desc.Service = methodInfo.DeclaringType.FullName; desc.ServiceComment = GetServiceComment(methodInfo); return(desc); }
private MemberDeclarationSyntax GenerateMethodDeclaration(MethodInfo method) { var serviceId = JimuHelper.GenerateServiceId(method); var returnDeclaration = GetTypeSyntax(method.ReturnType); var parameterList = new List <SyntaxNodeOrToken>(); var parameterDeclarationList = new List <SyntaxNodeOrToken>(); foreach (var parameter in method.GetParameters()) { if (parameter.ParameterType.IsGenericType) { parameterDeclarationList.Add(SyntaxFactory.Parameter( SyntaxFactory.Identifier(parameter.Name)) .WithType(GetTypeSyntax(parameter.ParameterType))); } else { parameterDeclarationList.Add(SyntaxFactory.Parameter( SyntaxFactory.Identifier(parameter.Name)) .WithType(GetQualifiedNameSyntax(parameter.ParameterType))); } parameterDeclarationList.Add(SyntaxFactory.Token(SyntaxKind.CommaToken)); parameterList.Add(SyntaxFactory.InitializerExpression( SyntaxKind.ComplexElementInitializerExpression, SyntaxFactory.SeparatedList <ExpressionSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.LiteralExpression( SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(parameter.Name)), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.IdentifierName(parameter.Name) }))); parameterList.Add(SyntaxFactory.Token(SyntaxKind.CommaToken)); } if (parameterList.Any()) { parameterList.RemoveAt(parameterList.Count - 1); parameterDeclarationList.RemoveAt(parameterDeclarationList.Count - 1); } MethodDeclarationSyntax declaration; if (method.ReturnType == typeof(void)) { declaration = SyntaxFactory.MethodDeclaration(SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)), SyntaxFactory.Identifier(method.Name)); } else { declaration = SyntaxFactory.MethodDeclaration( returnDeclaration, SyntaxFactory.Identifier(method.Name)); } if (method.ReturnType.Namespace == typeof(Task).Namespace) { declaration = declaration.WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.AsyncKeyword))); } else { declaration = declaration.WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword))); } declaration = declaration.WithParameterList(SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList <ParameterSyntax>(parameterDeclarationList))); ExpressionSyntax expressionSyntax; StatementSyntax statementSyntax; if (method.ReturnType.Namespace != typeof(Task).Namespace) { if (method.ReturnType == typeof(void)) { expressionSyntax = SyntaxFactory.IdentifierName("InvokeVoid"); } else { expressionSyntax = SyntaxFactory.GenericName( SyntaxFactory.Identifier("Invoke")) //.WithTypeArgumentList(((GenericNameSyntax)returnDeclaration).TypeArgumentList); .WithTypeArgumentList(SyntaxFactory.TypeArgumentList(SyntaxFactory.SingletonSeparatedList(returnDeclaration))) ; } expressionSyntax = SyntaxFactory.InvocationExpression(expressionSyntax) .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SeparatedList <ArgumentSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.Argument( SyntaxFactory.LiteralExpression( SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(serviceId))), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.ObjectCreationExpression( SyntaxFactory.GenericName( SyntaxFactory.Identifier("Dictionary")) .WithTypeArgumentList( SyntaxFactory.TypeArgumentList( SyntaxFactory.SeparatedList <TypeSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.PredefinedType( SyntaxFactory.Token(SyntaxKind.StringKeyword)), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.PredefinedType( SyntaxFactory.Token(SyntaxKind.ObjectKeyword)) })))) .WithInitializer( SyntaxFactory.InitializerExpression( SyntaxKind.CollectionInitializerExpression, SyntaxFactory.SeparatedList <ExpressionSyntax>( parameterList)))) }))); //expressionSyntax = SyntaxFactory.express } else { if (method.ReturnType == typeof(Task)) { expressionSyntax = SyntaxFactory.IdentifierName("InvokeVoidAsync"); } else { expressionSyntax = SyntaxFactory.GenericName( SyntaxFactory.Identifier("InvokeAsync")) .WithTypeArgumentList(((GenericNameSyntax)returnDeclaration).TypeArgumentList); //.WithTypeArgumentList(TypeArgumentList(SingletonSeparatedList(returnDeclaration))) } expressionSyntax = SyntaxFactory.AwaitExpression( SyntaxFactory.InvocationExpression(expressionSyntax) .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SeparatedList <ArgumentSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.Argument( SyntaxFactory.LiteralExpression( SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(serviceId))), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.ObjectCreationExpression( SyntaxFactory.GenericName( SyntaxFactory.Identifier("Dictionary")) .WithTypeArgumentList( SyntaxFactory.TypeArgumentList( SyntaxFactory.SeparatedList <TypeSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.PredefinedType( SyntaxFactory.Token(SyntaxKind.StringKeyword)), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.PredefinedType( SyntaxFactory.Token(SyntaxKind.ObjectKeyword)) })))) .WithInitializer( SyntaxFactory.InitializerExpression( SyntaxKind.CollectionInitializerExpression, SyntaxFactory.SeparatedList <ExpressionSyntax>( parameterList)))) })))); } if (method.ReturnType != typeof(Task) && method.ReturnType != typeof(void)) { statementSyntax = SyntaxFactory.ReturnStatement(expressionSyntax); } else { statementSyntax = SyntaxFactory.ExpressionStatement(expressionSyntax); } declaration = declaration.WithBody( SyntaxFactory.Block( SyntaxFactory.SingletonList(statementSyntax))); return(declaration); }