Regex C # - how to combine methods and properties of class names

I want to combine the name of the method (with parameters) and the name of the property. I do not want to include accessors in the match. For example, I got a class like this:

public class test
{
    public static string NA = "n/a";

    public static DateTime DateParser(string dateToBeParsed)
    {
        DateTime returnValue = new DateTime();
        DateTime.TryParse(dateToBeParsed, GetCulture(), System.Globalization.DateTimeStyles.AssumeLocal , out returnValue);
        return returnValue;
    }
}

For the class name, I use this regular expression: (? <= Class \ s) [^ \ s] + for im methods trying something like this: [^ \ s] + (? = () , But this will select the whole text with ( ). For methods, I need to select the line that has ( and Accessories, such as public, private and protected. How to do this without including this in the final match? I only need the method name and parameters in brackets.

+1
source share
3 answers

?

class Program
{
    static void Main(string[] args)
    {
        string assemblyName = Path.Combine(Path.GetTempPath(), string.Format("temp{0}.dll", Guid.NewGuid()));
        CSharpCodeProvider codeProvider = new CSharpCodeProvider();
        CompilerParameters compilerParameters = new CompilerParameters(new string[]
        {
            "System.dll",
            "Microsoft.CSharp.dll",
        }, assemblyName);

        CompilerResults cr = codeProvider.CompileAssemblyFromSource(compilerParameters, File.ReadAllText("Program.cs"));

        if (cr.Errors.Count > 0)
        {
            foreach (CompilerError error in cr.Errors)
            {
                Console.WriteLine(error.ErrorText);
            }
        }
        else
        {
            AppDomain appDomain = AppDomain.CreateDomain("volatile");
            Proxy p = (Proxy)appDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(Proxy).FullName);
            p.ShowTypesStructure(assemblyName);
            AppDomain.Unload(appDomain);
            File.Delete(assemblyName);
        }
        Console.ReadLine();
    }
}

public class Proxy : MarshalByRefObject
{
    public void ShowTypesStructure(string assemblyName)
    {
        Assembly assembly = Assembly.LoadFrom(assemblyName);
        Type[] types = assembly.GetTypes();
        foreach (Type type in types)
        {
            Console.WriteLine(type.Name);
            MethodInfo[] mis = type.GetMethods();
            foreach (MethodInfo mi in mis)
            {
                Console.WriteLine("\t" + mi.Name);
            }
        }
    }
}
+3

. Regex

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

methodName parameterType parameter.

:

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

.

. Irony , #.

, , Expresso, .

- -, .

Sorry for not providing "specific" help, such as "use this expression", but I think that if you want to avoid the analyzer, it really is a lot of work :)

+1
source

All Articles