- >发送到此树中的number3(数字5)“number3 = 5”
- >发送到树编号1(编号2)和编号2(编号2)中的方法“方法(2,2)”
- >从此方法返回值“int returnValue = Method(2,2)”
- >从number3“int someNumber = number3”返回值
static void Main(string[] args)
{
project1();
}
public static void project1()
{
var tree = CSharpSyntaxTree.ParseText(@"
class OurService
{
int number3;
int Method(int number1, int number2)
{
return number1 + number2 + number3;
}
}");
var diagnostics = tree.GetDiagnostics().Where(n => n.Severity == Microsoft.CodeAnalysis.DiagnosticSeverity.Error).FirstOrDefault();
var root = (CompilationUnitSyntax)tree.GetRoot();
var methodDeclareted = root.DescendantNodes().OfType<MethodDeclarationSyntax>().FirstOrDefault();
var parameters = methodDeclareted.ParameterList;
}
答案 0 :(得分:0)
我自己解决了这个问题。我想只添加这个&#34; Roslyn是很棒的工具。你可以删除IronPython和Python&#34;。我希望将来我会得到一些分数:P因为现在我得到了我的问题-1:P呵呵
public static void TestCodeAnalizer()
{
//var scriptOptions = ScriptOptions.Default.WithReferences("Microsoft.CSharp")
var text = @"using System.Linq;
public class Calculator
{
public int number3 = 0;
public static object AddTwoNumbers(int number1, int number2)
{
return (number1 + number2);
}
public static int AddThreeNumbers(int number1, int number2)
{
Calculator calculator = new Calculator();
return calculator.AddThreeNumbersNonStatic(number1,number2,5);
}
public int AddThreeNumbersNonStatic(int number1, int number2, int number3)
{
this.number3 = number3;
return (number1 + number2 + this.number3);
}
}";
var tree = SyntaxFactory.ParseSyntaxTree(text);
var compilation = CSharpCompilation.Create(
"calc.dll",
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
syntaxTrees: new[] { tree },
references: new[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location), MetadataReference.CreateFromFile(typeof(ExpandoObject).Assembly.Location) });
Assembly compiledAssembly;
using (var stream = new MemoryStream())
{
var compileResult = compilation.Emit(stream);
compiledAssembly = Assembly.Load(stream.GetBuffer());
}
Type calculator = compiledAssembly.GetType("Calculator");
//my send parameters :D
object[] parametersArray = new object[] { 2, 2 };
//first answer
MethodInfo evaluate_AddTwoNumbers = calculator.GetMethod("AddTwoNumbers");
string answer_AddTwoNumbers = evaluate_AddTwoNumbers.Invoke(null, parametersArray).ToString();
//second answer
MethodInfo evaluate_AddThreeNumbers = calculator.GetMethod("AddThreeNumbers");
string answer_AddThreeNumbers = evaluate_AddThreeNumbers.Invoke(null, parametersArray).ToString();
}