Regex: C # method declaration parsing

Can someone help me parse from the C # method description: scope, isStatic, name, return type, and a list of parameters and their types. So, this method declaration like this

public static SomeReturnType GetSomething(string param1, int param2)

etc .. I need to analyze it and get the information above. So in this case

  • name = "GetSomething"
  • scope = "public"
  • isStatic = true
  • returnType = "SomeReturnType"

and then an array of parameter pairs and name pairs.

Oh, I almost forgot about the most important part. It should take into account all other areas (protected, private, internal, protected internal), the absence of a "static", invalid return type, etc.

Please note that REFLECTION is not a solution. I need a REGEX.

So far I have these two:

 (?:(?:public)|(?:private)|(?:protected)|(?:internal)|(?:protected internal)\s+)*

(?:(?:static)\s+)*

, .

+3
5

:

, , . , , #. , # #, .

, , . (, .) , . . ( - , .)

, . :

  • generic/array/pointer/nullable return
  • //////
  • //
  • - , -

, , , , , . .

? - . , , ?

+6

Regex. , (, ref out). , , .

, #. :

, . , . , , , , .

- :

List<string> referenceAssemblies = new List<string>()
{
    "System.dll"
    // ...
};

string source = "public abstract class TestClass {" + input + ";}";

CSharpCodeProvider codeProvider = new CSharpCodeProvider();

// No assembly name specified
CompilerParameters compilerParameters =
    new CompilerParameters(referenceAssemblies.ToArray());
compilerParameters.GenerateExecutable = false;
compilerParameters.GenerateInMemory = false;

CompilerResults compilerResults = codeProvider.CompileAssemblyFromSource(
    compilerParameters, source);

// Check for successful compilation here

Type testClass = compilerResults.CompiledAssembly.GetTypes().First();

testClass.

, - . , , , .

+1
string test = @"public static SomeReturnType GetSomething(string param1, int param2)";
var match = Regex.Match(test, @"(?<scope>\w+)\s+(?<static>static\s+)?(?<return>\w+)\s+(?<name>\w+)\((?<parms>[^)]+)\)");
Console.WriteLine(match.Groups["scope"].Value);
Console.WriteLine(!string.IsNullOrEmpty(match.Groups["static"].Value));
Console.WriteLine(match.Groups["return"].Value);
Console.WriteLine(match.Groups["name"].Value);
List<string> parms = match.Groups["parms"].ToString().Split(',').ToList();
parms.ForEach(x => Console.WriteLine(x));
Console.Read();

, .

0
(?<StringRepresentation>\A\s*(?:(?:(?<Comment>(?://.*\n)|(?:/\*(?:[\w\d!@#$%^&*()\[\]<>,.;\\"':|{}`~+=-_?\s]*)?\*/))|(\[\s*(?<Attributes>\w*)[^\[\]]*?\]))\s*)*?(?:(?:(?<Access>protected\s+internal|internal\s+protected|private|public|protected|internal)\s+)?(?:(?<InheritanceModifier>new|abstract|override|virtual)\s+)?(?:(?<Static>static)\s+)?(?:(?<Extern>extern)\s+)?(?:partial\s+)?)+(?:(?<Type>\w+(?:[\w,.\?\[\]])*?(?:\<.*>)*?)\s+)?(?<Operator>operator\s+)?\s*(?<Name>~?(?:[\w\=+\-\!\~\d\.])+?)\s*(?:\<(?:\w\.*\d*\,*\s*)+\>)*\s*\((?<Parameters>(?:[^()])*?)\)\s*(?:where\s+.+)?\s*(?:\:\s*(?:this|base)\s*(?:\(?[^\(\)]*(?:(?:(?:(?<OpenC>\()[^\(\)]*)+(?:(?<CloseC-OpenC>\))[^\(\)]*?)+)*(?(OpenC)(?!))\)))\s*)?(?:;|(?<ah>\{[^\{\}]*(?:(?:(?:(?<Open>\{)[^\{\}]*)+(?:(?<Close-Open>\})[^\{\}]*?)+)*(?(Open)(?!))\}))))

, , Regionerate ( ), .

0

Source: https://habr.com/ru/post/1730389/


All Articles