C#判断代码片段为class还是method

打印 上一主题 下一主题

主题 504|帖子 504|积分 1512

判断代码片段是class还是method:
  1. public partial class Form1 : Form
  2.     {
  3.         public Form1()
  4.         {
  5.             InitializeComponent();
  6.         }
  7.         private void button1_Click(object sender, EventArgs e)
  8.         {
  9.             string codeSnippet1 = @"public class MyClass
  10.             {
  11.                 bool b=false;
  12.                  public    void   MyMethod()
  13.                 {
  14.                     int i=0;
  15.                     // Method logic
  16.                 }
  17.             }";
  18.             string codeSnippet2 = @"public void MyMethod()
  19.             {
  20.                 // Method logic
  21.             }
  22.             public void MyMethod1()
  23.             {
  24.                 // Method logic
  25.             }
  26. ";
  27.             Console.WriteLine(IsClassOrFunction(codeSnippet1)); // Output: Class
  28.             Console.WriteLine(IsClassOrFunction(codeSnippet2)); // Output: Function
  29.         }
  30.         static string IsClassOrFunction(string codeSnippet)
  31.         {
  32.             // Regular expression to match a class definition
  33.             var classRegex = new Regex(@"^\s*public\s+class\s+\w+", RegexOptions.Multiline);
  34.             // Regular expression to match a method definition
  35.             var methodRegex = new Regex(@"^\s*(public|private|protected|internal)?\s*(static\s+)?\w+\s+\w+\s*\(.*\)\s*\{", RegexOptions.Multiline);
  36.             if (classRegex.IsMatch(codeSnippet))
  37.             {
  38.                 return "Class";
  39.             }
  40.             else if (methodRegex.IsMatch(codeSnippet))
  41.             {
  42.                 return "Function";
  43.             }
  44.             else
  45.             {
  46.                 return "Unknown";
  47.             }
  48.         }
  49.     }
复制代码

抽取class代码中的成员变量和成员方法:
  1. using System;
  2. using System.Linq;
  3. using Microsoft.CodeAnalysis;
  4. using Microsoft.CodeAnalysis.CSharp;
  5. using Microsoft.CodeAnalysis.CSharp.Syntax;
  6. class Program
  7. {
  8.     static void Main()
  9.     {
  10.         string codeSnippet = @"
  11.         public class MyClass
  12.         {
  13.             public int MyField;
  14.             private string _myPrivateField;
  15.             public void MyMethod()
  16.             {
  17.                 // Method logic
  18.             }
  19.             private void MyPrivateMethod()
  20.             {
  21.                 // Method logic
  22.             }
  23.         }";
  24.         AnalyzeClass(codeSnippet);
  25.     }
  26.     static void AnalyzeClass(string codeSnippet)
  27.     {
  28.         // Parse the code snippet
  29.         var tree = CSharpSyntaxTree.ParseText(codeSnippet);
  30.         var root = tree.GetRoot();
  31.         // Find the class declaration
  32.         var classDeclaration = root.DescendantNodes()
  33.             .OfType<ClassDeclarationSyntax>()
  34.             .FirstOrDefault();
  35.         if (classDeclaration != null)
  36.         {
  37.             Console.WriteLine($"Class: {classDeclaration.Identifier.Text}");
  38.             // Extract fields (member variables)
  39.             var fields = classDeclaration.Members
  40.                 .OfType<FieldDeclarationSyntax>();
  41.             Console.WriteLine("Fields:");
  42.             foreach (var field in fields)
  43.             {
  44.                 var fieldType = field.Declaration.Type.ToString();
  45.                 foreach (var variable in field.Declaration.Variables)
  46.                 {
  47.                     Console.WriteLine($"  {fieldType} {variable.Identifier.Text}");
  48.                 }
  49.             }
  50.             // Extract methods
  51.             var methods = classDeclaration.Members
  52.                 .OfType<MethodDeclarationSyntax>();
  53.             Console.WriteLine("Methods:");
  54.             foreach (var method in methods)
  55.             {
  56.                 var returnType = method.ReturnType.ToString();
  57.                 Console.WriteLine($"  {returnType} {method.Identifier.Text}");
  58.             }
  59.         }
  60.         else
  61.         {
  62.             Console.WriteLine("No class declaration found.");
  63.         }
  64.     }
  65. }
复制代码




免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

小小小幸运

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表