Roslyn VisualBasic.ScriptEngine does not recognize hostObject written in C #

Our project should have simple business rules that our clients can script in Visual Basic. Although our main program is written in C #

the script that clients want to execute may be like this (I am considering the simplest case)

var vbCode = @" If (Row.Code = 12) Then Row.MappedCode = 1 End If"; 

So, I created a RowData class in C # with the Code and MappedCode properties

 namespace ScriptModel { public class RowData { public int Code { get; set; } public int MappedCode { get; set; } } } 

I created a simple class of class objects, for example

 namespace ScriptModel { public class HostObjectModel { public RowData Row { get; set; } } } 

Using Roslyn.Scripting.VisualBasic.ScriptEngine I create an engine, create a session with an instance of HostObjectModel and execute engine.Execute (vbCode, session)

  var hostObj = new HostObjectModel(); hostObj.Row = new RowData(); hostObj.Row.Code = 12; var engine = new Roslyn.Scripting.VisualBasic.ScriptEngine( new Assembly[] {hostObj.GetType().Assembly}, new string[] {"ScriptModel"} ); var session = Session.Create(hostObj); engine.Execute(vbCode , session); 

And he tells me that

(2.25): error BC30451: "String" was not declared. This may not be available due to its level of protection.

But if I create a similar code snippet in C #

  var csharpCode = @" if (Row.Code == 12) { Row.MappedCode = 1; };"; 

and use CSharp.ScriptEngine, everything will work correctly

So what is the problem, why was VisualBasic.ScriptEngine unable to see the public properties of the class that was compiled in C #, should it be, I think, in the same MSIL language, or am I mistaken?


Update: I installed Visual Basic and created the ScriptModel library on VB. I also replaced the Row property with the Row () function in both the class declaration and vbCode. Nothing helped. :( it seems that VisualBasic.ScriptEngine doesn't work at all when I run it from C #.

+4
source share
2 answers

For VB scripts, I found that you should include the following at the beginning of the script:

 Imports ScriptModel 

I assume that you can automatically pre-substitute the above line of code so that your users do not need to enable it.

I was not able to get it to work when it was added as part of the ScriptEngine creation. It seems like after that it doesn't work using:

 engine.ImportedNamespaces.Append("ScriptModel"); 

This is despite the fact that subsequently the number of ImportedNamespaces is 1. With C #, you don't seem to need to import the namespace at all.

+3
source

I accepted the previous answer because it really gave me an idea on how to make the VB.NET script work, however HostObject still not working

So the actual workaround consists of two steps

1) Use Imports ScriptModel in VB Code

 var vbCode = @"Imports ScriptModel If (Row.Code = 12) Then Row.MappedCode = 1 End If"; 

2) Do not use HostObject. Define a string as an open static class

 namespace ScriptModel { public static class Row { public static int Code { get; set; } public static int MappedCode { get; set; } } } 

I believe the answer on the MS forum is also correct, http://social.msdn.microsoft.com/Forums/en-US/roslyn/thread/89970f0b-1c1c-47da-a180-9c4710abc4b9 in the current version of HostObject is not supported for VB but I hope this will be supported in the next version

+1
source

All Articles