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
With this code, I replace global:: with simple types:
}
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.