skip to Main Content

When I extract method from this code, final result have global::System.Int32[] list as parameter in NewMethod:

using System;
public class Program
{
    public static void Main(string[] args)
    {
        var list = new int[] {3, 2, 1 };

        foreach(var i in list) Console.WriteLine(i);
    }
}

Final result:

using System;
public class Program
{
    public static void Main(string[] args)
    {
        var list = new int[] { 3, 2, 1 };
        NewMethod(list);
    }

    private static void NewMethod(**global::System.Int32[] list**)
    {
        foreach (var i in list) Console.WriteLine(i);
    }
}

I use this this Roslyn class to extract method:

var options = new ExtractMethodGenerationOptions()
    {CodeGenerationOptions = CodeGenerationOptions.GetDefault(document.Project.Services),
    ExtractOptions = new() { DontPutOutOrRefOnStruct = true}}

var service = new CSharpExtractMethodService() as IExtractMethodService;
var extract = await service.ExtractMethodAsync(document, span, false,options, CancellationToken.None);
var result = extract.DocumentWithoutFinalFormatting;

Why I have global::, when I extract method with Visual Studio, the NewMethod have simple int[] list as method parameter?

2

Answers


  1. Chosen as BEST ANSWER

    With this code, I replace global:: with simple types:

    public class MySimplifier
        {
            public static async Task<Document> SimplifyTypes(Document document)
            {
                var root = await document.GetSyntaxRootAsync().ConfigureAwait(false);
                var editor = await DocumentEditor.CreateAsync(document).ConfigureAwait(false);
    
            var nodesToReplace = root.DescendantNodesAndSelf()
                .OfType<TypeSyntax>()
                .Where(n => n.ToString().StartsWith("global::"))
                .ToList();
    
            foreach (var node in nodesToReplace)
            {
                var typeName = node.ToString().Substring("global::".Length);
                var simpleType = GetSimpleType(typeName);
                if (simpleType != null)
                {
                    var newType = SyntaxFactory.ParseTypeName(simpleType);
                    editor.ReplaceNode(node, newType);
                }
            }
    
            return editor.GetChangedDocument();
        }
    
        private static string GetSimpleType(string typeName)
        {
            switch (typeName)
            {
                case "System.Boolean":
                    return "bool";
                case "System.Byte":
                    return "byte";
                case "System.Char":
                    return "char";
                case "System.Decimal":
                    return "decimal";
                case "System.Double":
                    return "double";
                case "System.Int16":
                    return "short";
                case "System.Int32":
                    return "int";
                case "System.Int64":
                    return "long";
                case "System.SByte":
                    return "sbyte";
                case "System.Single":
                    return "float";
                case "System.String":
                    return "string";
                case "System.UInt16":
                    return "ushort";
                case "System.UInt32":
                    return "uint";
                case "System.UInt64":
                    return "ulong";
                default:
                    return null;
            }
        }
    }
    

    }


  2. Take the final document and then run it through our Simplifier API. Generally, when we do refactorings, we internally expand names to fully qualified things (global:: and all) and the as final pass clean everything back up. This allows us to potentially move that code around to other files or places where qualification may have to be different.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search