1 -我在光盘上有这个文件内容(cs文件,未编译):
namespace Test
{
using System;
public class TestClass
{
public string SomeTestMethod(){
return "test here";
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何将运行时间输入变量方法:
public string SomeTestMethod(){
return "test here";
}
Run Code Online (Sandbox Code Playgroud)
例如: SourceCodeParser.GetMothod("path to file","SomeTestMethod");
2 - 访问者成员的内容是否可能?
public string SomeMember {
get {
return "test here";
}
}
Run Code Online (Sandbox Code Playgroud)
Kon*_*osa 13
罗斯林是你需要的.您可以使用nuget轻松安装它.这是获取方法体的工作代码:
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();
}
Run Code Online (Sandbox Code Playgroud)
以及获取属性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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3796 次 |
| 最近记录: |