我有一个小问题。我正在使用ROSLYN编译器,并尝试调用
RoslynTest.ComputeClass.Compute方法,但是出现运行时错误 “ System.MissingMethodException:'方法 找不到RoslynTest.ComputeClass.Compute。简而言之 RoslynTest.RoslynInterface中的变量g_code中的代码
MS VS 2019中的InitCompiler和complette源代码在这里:
https://drive.google.com/file/d/1TRKshJhApC2ByC961I9f__axjTyGCdnm/view?usp=sharing
以及下面的文本中。由于将数组类型变量传递给
中的方法而导致了问题InvokeCode(ref MyData[,] t_array )
如果是简单变量,例如使用Int32 A代替“ MyData [,] t_array”,不会出现“ MissingMethodException”。
此问题是由于将数组类型变量传递给
中的方法而引起的InvokeCode(ref MyData[,] t_array )
如果是简单变量,例如使用Int32 A代替“ MyData [,] t_array”,不会出现“ MissingMethodException”。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Reflection; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Emit; namespace RoslynTest { public class MyData { public Int32 x; public Int32 y; }// public class MyData public class RoslynInterface { public String g_code; SyntaxTree g_syntaxTree; string g_assemblyName; CSharpCompilation g_compilation; MetadataReference[] g_references; MemoryStream g_ms; EmitResult g_result; Assembly g_assembly; Type g_type; object g_obj; IEnumerable g_failures; Object g_return_Object; Type g_return_Type; public String g_errorMessages; public Boolean InitCompiler() { g_code = @" using System; namespace RoslynTest { public class MyData { public Int32 x; public Int32 y; }// public class MyData public class ComputeClass { public Boolean Compute(ref MyData[,] t_array) { t_array[0, 0].x = 20; t_array[0, 0].y = 20; return false; }// }// public class ComputeClass }// namespace RoslynTest "; g_syntaxTree = CSharpSyntaxTree.ParseText(@g_code); g_assemblyName = Path.GetRandomFileName(); g_references = new MetadataReference[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location), MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location) }; g_compilation = CSharpCompilation.Create(g_assemblyName, syntaxTrees: new[] { g_syntaxTree }, references: g_references, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); g_ms = new MemoryStream(); g_result = g_compilation.Emit(g_ms); if (!g_result.Success) { g_failures = g_result.Diagnostics.Where( diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error); g_errorMessages = ""; foreach (Diagnostic diagnostic in g_failures) { g_errorMessages = g_errorMessages + diagnostic.Id.ToString() + " " + diagnostic.GetMessage() + System.Environment.NewLine; }// foreach } else { g_ms.Seek(0, SeekOrigin.Begin); g_assembly = Assembly.Load(g_ms.ToArray()); g_type = g_assembly.GetType("RoslynTest.ComputeClass"); g_obj = Activator.CreateInstance(g_type); }// if else return g_result.Success; }// public Boolean CreateBWImage() public Boolean InvokeCode(ref MyData[,] t_array ) { g_return_Object = g_type.InvokeMember("Compute", BindingFlags.InvokeMethod, // BindingFlags.Default | null, g_obj, new object[] { t_array }); g_return_Type = g_return_Object.GetType(); return (Boolean)g_return_Object; }// public Boolean InvokeCode() }// public class RoslynCompiler public class Program { static void Main(string[] args) { MyData[,] md = new MyData[5, 5]; for (Int32 x = 0; x
我收到运行时错误“ System.MissingMethodException:'找不到方法RoslynTest.ComputeClass.Compute。'