Exemple #1
0
        private static void AnalyzeSymbol(SymbolAnalysisContext context)
        {
            var method = context.Symbol as IMethodSymbol;

            var mappingModel = MappingModelBuilder.GetTypeInfo(method);

            if (mappingModel == null)
            {
                return;
            }

            var startIndex        = context.Symbol.Locations.First().SourceSpan.Start;
            var methodDeclaration = method.DeclaringSyntaxReferences.First();

            var semanticModel = context.Compilation.GetSemanticModel(methodDeclaration.SyntaxTree);

            string GetSymbolDisplayString(ISymbol symbol) => symbol.ToMinimalDisplayString(semanticModel, startIndex);

            if (mappingModel.MemberPairs.Any(member => !member.IsImplemented && member.Target != null && (member.Source != null || member.IsImplemented)))
            {
                context.ReportDiagnostic(Diagnostic.Create
                                         (
                                             Rule,
                                             method.Locations[0],
                                             GetSymbolDisplayString(mappingModel.SourceType),
                                             GetSymbolDisplayString(mappingModel.TargetType)
                                         ));
            }
        }
        private MethodDeclarationSyntax CreateMapMethod(MethodDeclarationSyntax oldMethod, SemanticModel semanticModel)
        {
            var parameter = oldMethod.ParameterList.Parameters[0] as ParameterSyntax;

            var mappingModel = MappingModelBuilder.GetTypeInfo(semanticModel.GetDeclaredSymbol(oldMethod));

            var preMapped    = GetExistingArguments(oldMethod);
            var argumentList = GetArgumentList
                               (
                sourceName: parameter.Identifier.Text,
                typeInfo: mappingModel,
                existingArguments: preMapped
                               );

            var newMethod = oldMethod.WithBody(SyntaxFactory.Block(SyntaxFactory.ReturnStatement(SyntaxFactory.ObjectCreationExpression
                                                                                                 (
                                                                                                     type: oldMethod.ReturnType,
                                                                                                     argumentList: argumentList,
                                                                                                     initializer: null
                                                                                                 )))).WithAdditionalAnnotations(Formatter.Annotation);

            return(newMethod);
        }