C # regex - select class property names, method name and fields from class file (.cs)

I want to match (select from class file) the method name, property name and field name.

This is an example class:

class Perl
{
    string _name;
    public string Name { get; set; }
    public Perl()
    {
    // Assign this._name
    this._name = "Perl";
    // Assign _name
    _name = "Sam";

    // The two forms reference the same field.
    Console.WriteLine(this._name);
    Console.WriteLine(_name);
    }
    public static string doSomething(string test)
    {
        bla test;
    }
}

I got the code for the methods:

(?:public|private|protected)([\s\w]*)\s+(\w+)\s*\(\s*(?:\w+\s+(\w+)\s*,?\s*)+\)

And I had questions:

  • this regex code gets all the methods and it works very well, but I also want it to select the method name, but without parameters and accessors. Therefore, from the exaplmce class using my code, the result will be: public Perl () and public static doSomething (string test) , but I want to get this result: Perl () and doSomething () . So - my code is good, but I want the result to be displayed exactly as I wrote in the previous sentence.
  • ? : . , exaple :
  • : field_name. : _name
+5
4

Regex

(?:public\s|private\s|protected\s|internal\s)?[\s\w]*\s+(?<methodName>\w+)\s*\(\s*(?:(ref\s|/in\s|out\s)?\s*(?<parameterType>\w+)\s+(?<parameter>\w+)\s*,?\s*)+\)

methodName parameterType parameter.

:

(?:public\s|private\s|protected\s)\s*(?:readonly\s+)?(?<type>\w+)\s+(?<name>\w+)

type name.

, :

var inputString0 = "public void test(string name, out int value)\r\nvoid test(string name, int value)";
foreach (Match match in Regex.Matches(inputString0, @"(?:public\s|private\s|protected\s)?[\s\w]*\s+(?<methodName>\w+)\s*\(\s*(?:(ref\s|/in\s|out\s)?\s*(?<parameterType>[\w\?\[\]]+)\s+(?<parameter>\w+)\s*,?\s*)+\)"))
{
    var methodName = match.Groups["methodName"].Value;
    var typeParameterPair = new Dictionary<string, string>();
    int i = 0;
    foreach (var capture in match.Groups["parameterType"].Captures)
    {
        typeParameterPair.Add(match.Groups["parameterType"].Captures[i].Value, match.Groups["parameter"].Captures[i].Value);
        i++;
    }
}

" - .NET" .

+3

, .cs , . :

: csc.exe CodeDOM - , , .

+2

, #, , . , .

Roslyn : #, . Roslyn , .

0

Microsoft.VisualStudio.CSharp.Services.Language Visual Studio. .

0

All Articles