Exemple #1
0
        private async Task <Document> AddCloneImplementation(Document document, TypeDeclarationSyntax typeDeclaration, CancellationToken cancellationToken)
        {
            var generator     = SyntaxGenerator.GetGenerator(document);
            var semanticModel = await document.GetSemanticModelAsync(cancellationToken);

            //TODO: If method exists, replace it
            var newClassDeclaration = typeDeclaration.AddMethods(new[]
            {
                GenerateCloneMethodStronglyTyped(generator, typeDeclaration, semanticModel),
                GenerateCloneMethod(generator)
            });

            if (newClassDeclaration.BaseList == null || newClassDeclaration.BaseList.Types.Any(x => x.Type.ToString().Contains("ICloneable")) == false)
            {
                var cloneableInterface = SyntaxFactory.ParseTypeName($"System.ICloneable");
                newClassDeclaration = generator.AddInterfaceType(newClassDeclaration, cloneableInterface.WithAdditionalAnnotations(Formatter.Annotation)) as TypeDeclarationSyntax;
            }

            return(await document.ReplaceNodes(typeDeclaration, newClassDeclaration, cancellationToken));
        }