c# regex - select class properties names, methods name and fields from class file (.cs)

走远了吗. 提交于 2019-12-07 05:06:31

问题


I want to match (select from class file) methodsname, properties name and fields name.

This is 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 code for the methods:

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

And i got questions:

  • this above regex code gets all methods and it works pretty well but also i want it to select method name but without parameters and accessors. So from exaplmce class using my code result will be: public Perl() and public static doSomething(string test) but i want that kind of result: Perl() and doSomething(). So - my code matches good but i want result to be displayed just like I wrote in previous sentence.
  • how to select properties ? with result displayed: type and property name. So from exaple class result will be: string Name
  • how to select fields with result: type field_name. In out case it will be: string _name

回答1:


Use this Regex

for methods

(?: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*)+\)

and get groups named methodName and parameterType and parameter.

and for fields:

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

and get groups named type and name.

for example your code for methods can be like this:

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

You can use Irony - .NET Language Implementation Kit from codeplex too.




回答2:


As stated in comments to your answer, much more reliable method is to compile your .cs files and then use reflection to interrogate types for members you are interested in. It will involve the following:

  1. Use C# compiler (csc.exe) to compile your files. MSDN contains helpful examples and hints. You could do this manually, or, if you want to automatize the whole thing, you could execute csc.exe programmatically with Process class.
  2. Use Assembly.LoadFile() method to load the resulting assembly.
  3. You can now employ reflection to get types from the assembly and interrogate them.

EDIT: As an alternative to csc.exe, you could use CodeDOM - there is an example that contains all you need.




回答3:


A language such as C# accepts too many variations in statements syntax to be parsed using regular expressions only. On top of regexes, you need a contextual grammar parser.

I would give Roslyn a try: It's a C# compiler whose internals are accessible from your code. Ask Roslyn to parse the code and query it about whatever info you need.




回答4:


I suggest looking at Microsoft.VisualStudio.CSharp.Services.Language namespace and other Visual Studio Extensibility functionality. This would eliminate the need to compile.



来源:https://stackoverflow.com/questions/11914473/c-sharp-regex-select-class-properties-names-methods-name-and-fields-from-clas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!