C# 如何在类中提取完整的方法?

C# 如何在类中提取完整的方法?,c#,text,methods,C#,Text,Methods,我试图提取cs文件中的完整方法。 例如。。假设我们有一个这样的班 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GetMethodNm { class MyClass { public int ComplexMethod(int param1, int myCustomValue)

我试图提取cs文件中的完整方法。 例如。。假设我们有一个这样的班

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GetMethodNm
{
class MyClass
{

    public int ComplexMethod(int param1, int myCustomValue)
    {
        if (param1 == myCustomValue)
        {
            return 54;
        }
        else
        {
            return 0;
        }
    }

    public string ComplexMethodV(int param1, int myCustomValue)
    {
        if (param1 < myCustomValue)
        {
            return "300";
        }
        else
        {
            return "My custom value to return";
        }
    }

    public bool ComplexMethodX(int param1, int myCustomValue)
    {
        if (param1 == myCustomValue)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间GetMethodNm
{
类MyClass
{
公共int ComplexMethod(int参数1,int myCustomValue)
{
如果(参数1==myCustomValue)
{
返回54;
}
其他的
{
返回0;
}
}
公共字符串ComplexMethodV(int参数1,int myCustomValue)
{
如果(参数1

然后我需要提取读取cs文件的方法ComplexMethodV。。我该怎么做?我尝试过反思,但我只能知道它的名字和里面的一些东西。。但是我需要里面的literal方法。

使用Roslyn这样的任务相对容易。在项目中添加NuGet包,然后只需使用以下代码

using System;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace SandboxConsole
{
    class Program
    {
        public static void Main()
        {
            var text = File.ReadAllText("MyClass.cs");
            var tree = CSharpSyntaxTree.ParseText(text);

            var method = tree.GetRoot().DescendantNodes()
                         .OfType<MethodDeclarationSyntax>()
                         .First(x => x.Identifier.Text == "ComplexMethodV");

            Console.WriteLine(method.ToFullString());
            Console.ReadLine();
        }
    }
}
使用系统;
使用System.IO;
使用System.Linq;
使用Microsoft.CodeAnalysis.CSharp;
使用Microsoft.CodeAnalysis.CSharp.Syntax;
名称空间沙盒控制台
{
班级计划
{
公共静态void Main()
{
var text=File.ReadAllText(“MyClass.cs”);
var-tree=CSharpSyntaxTree.ParseText(文本);
var method=tree.GetRoot().genderantNodes()
第()类
.First(x=>x.Identifier.Text==“ComplexMethodV”);
WriteLine(方法.ToFullString());
Console.ReadLine();
}
}
}
这将输出文本

        public string ComplexMethodV(int param1, int myCustomValue)
        {
            if (param1 < myCustomValue)
            {
                return "300";
            }
            else
            {
                return "My custom value to return";
            }
        }
公共字符串ComplexMethodV(int-param1,int-myCustomValue)
{
如果(参数1

有关如何解析整个解决方案等操作的更多高级教程,请参阅。

是否阅读源代码?为什么不使用
FileStream
简单地读取文件本身?要读取整个定义吗?要调用该方法还是其他方法?您希望在运行前清除所有参数,还是?目的何在?有时,如果你提供更多的上下文,人们可以更好地帮助你。你所说的摘录是什么意思?反射只提供元数据,而不提供源代码。您需要对二进制文件进行反编译并搜索该方法。您必须让我们知道您的最终目标,因为答案可能会有很大差异。谢谢。。有没有办法修改此方法并将节点替换到树中?欢迎提供任何线索是的,