J
Jerry.Mouse
Guest
Hello,
I have a small problem. I am using ROSLYN compiler and tried to invoke RoslynTest.ComputeClass.Compute method, but I got run-time error "System.MissingMethodException: 'Method RoslynTest.ComputeClass.Compute was not found.' The method is in short code in variable g_code in RoslynTest.RoslynInterface.InitCompiler and complette source code in MS VS 2019 is here:
RoslynTest.zip
and also in in text below. The problem is caused thanks to array-type variable passing to method in
InvokeCode(ref MyData[,] t_array )
if simple variable e.g. Int32 A is used instead of "MyData[,] t_array" no "MissingMethodException" arose.
Can someone help me where is problem ?
Many thanks
Jerry
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<Diagnostic> 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 < 5; x++)
{
for (Int32 y = 0; y < 5; y++)
{
MyData item = new MyData();
item.x = x;
item.y = y;
md[x, y] = item;
}// for
}// for
RoslynInterface iface = new RoslynInterface();
iface.InitCompiler();
iface.InvokeCode(ref md);
iface = null;
md = null;
}// static void Main(string[] args)
}// public class Program
}// namespace RoslynTest
Continue reading...
I have a small problem. I am using ROSLYN compiler and tried to invoke RoslynTest.ComputeClass.Compute method, but I got run-time error "System.MissingMethodException: 'Method RoslynTest.ComputeClass.Compute was not found.' The method is in short code in variable g_code in RoslynTest.RoslynInterface.InitCompiler and complette source code in MS VS 2019 is here:
RoslynTest.zip
and also in in text below. The problem is caused thanks to array-type variable passing to method in
InvokeCode(ref MyData[,] t_array )
if simple variable e.g. Int32 A is used instead of "MyData[,] t_array" no "MissingMethodException" arose.
Can someone help me where is problem ?
Many thanks
Jerry
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<Diagnostic> 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 < 5; x++)
{
for (Int32 y = 0; y < 5; y++)
{
MyData item = new MyData();
item.x = x;
item.y = y;
md[x, y] = item;
}// for
}// for
RoslynInterface iface = new RoslynInterface();
iface.InitCompiler();
iface.InvokeCode(ref md);
iface = null;
md = null;
}// static void Main(string[] args)
}// public class Program
}// namespace RoslynTest
Continue reading...