获取特定方法的源代码cs文件(在运行时) [英] Getting a specific method source code from .cs file (at runtime)

查看:190
本文介绍了获取特定方法的源代码cs文件(在运行时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1 - 我在光盘上这个文件的内容(CS文件,而不是编译):

 命名空间测试
{
使用系统;
公共类识别TestClass
{
公共字符串SomeTestMethod(){
回归测试在这里;
}
}
}



我如何在运行时间到一个变量方法:

 公共字符串SomeTestMethod(){
回归测试在这里;
}

例如: SourceCodeParser.GetMothod(路径文件,SomeTestMethod);



2 - 是否有可能访问成员的含量

 公共字符串SomeMember {
获得{
回归测试在这里;
}
}


解决方案

< A HREF =htt​​p://msdn.microsoft.com/en-us/vstudio/roslyn.aspx>罗斯林是你所需要的。您可以使用的NuGet 容易安装。下面是获得方法体工作代码:

 字符串实现getMethod(字符串的文件名,字符串methodName中)
{
VAR syntaxTree = SyntaxTree.ParseFile(文件名);
无功根= syntaxTree.GetRoot();
VAR方法= root.DescendantNodes()
.OfType< MethodDeclarationSyntax>()
。凡(MD = GT; md.Identifier.ValueText.Equals(方式))
。 FirstOrDefault();
返回method.ToString();
}

和获取属性的getter的机身码:

 字符串GetPropertyGetter(字符串的文件名,字符串propertyName的)
{
VAR syntaxTree = SyntaxTree.ParseFile(文件名);
无功根= syntaxTree.GetRoot();
VAR财产= root.DescendantNodes()
.OfType< PropertyDeclarationSyntax>()
。凡(MD = GT; md.Identifier.ValueText.Equals(propertyName的))
。 FirstOrDefault();
变种吸气= property.AccessorList.Accessors.First(一个= GT; a.Kind == SyntaxKind.GetAccessorDeclaration);
返回getter.ToString();
}


1 - I have this file content on the disc (cs file, not compiled):

    namespace Test
    {
        using System;
        public class TestClass 
        {
            public string SomeTestMethod(){
                return "test here";
            }
        }
    }

How do I get in run time into a variable the method:

public string SomeTestMethod(){
    return "test here";
}

for example: SourceCodeParser.GetMothod("path to file","SomeTestMethod");

2 - Is it possible the content of accessor member?

   public string SomeMember {
        get {
            return "test here";
        }
    }

解决方案

Roslyn is what you need. You can easily install it using nuget. Here is a working code for getting a method body:

string GetMethod(string filename, string methodName)
{
    var syntaxTree = SyntaxTree.ParseFile(filename);
    var root = syntaxTree.GetRoot();
    var method = root.DescendantNodes()
                     .OfType<MethodDeclarationSyntax>()
                     .Where(md => md.Identifier.ValueText.Equals(methodName))
                     .FirstOrDefault();
    return method.ToString();
}

and code for getting body of property getter:

string GetPropertyGetter(string filename, string propertyName)
{
    var syntaxTree = SyntaxTree.ParseFile(filename);
    var root = syntaxTree.GetRoot();
    var property = root.DescendantNodes()
                       .OfType<PropertyDeclarationSyntax>()
                       .Where(md => md.Identifier.ValueText.Equals(propertyName))
                       .FirstOrDefault();
    var getter = property.AccessorList.Accessors.First(a => a.Kind == SyntaxKind.GetAccessorDeclaration);
    return getter.ToString();
}

这篇关于获取特定方法的源代码cs文件(在运行时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆