0

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?

3
  • 1
    look at stackoverflow.com/questions/9443357/… Commented Apr 5, 2023 at 16:57
  • Thank you Frenchy, but how I can get only simple types (int[]), and not global::Int32[]? :) Commented Apr 5, 2023 at 20:08
  • 1
    try to add a namespace to your class Commented Apr 5, 2023 at 20:13

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I try with this Simplifier: var doc = extract.DocumentWithoutFinalFormatting; var simple = await Simplifier.ReduceAsync(doc!); But I get same result, parameters with global::System.Int32. Maybe I should define some OptionSet in ReduceAsync call?
0

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;
        }
    }
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.